Classic vbs code

Source: Internet
Author: User
The following is a classic vbs Code Including Windows 2000 management, encoding, decoding, and so on...
I hope you will also like vbs.

Log out/restart/Close the local Windows NT/2000 computer

Sub Shutdown ()
Dim connection, WQL, systemclass, System

'Get connection to local WMI
Set connection = GetObject ("winmgmts: Root \ cimv2 ")

'Get win32_operatingsystem objects-only one object in the collection
WQL = "Select name from win32_operatingsystem"
Set systemclass = connection. execquery (WQL)

'Get one System Object
'I think there is no way to get the object using URL?
For each system in systemclass
System. win32shutdown (2)
Next
End sub

Log out/restart/Close Remote Windows NT/2000 computer

Sub shutdownex (server, user, password) dim connection, WQL, systemclass, system 'get connection to remote WMI dim locator set locator = Createobject ("wbemscripting. swbemlocator ") set connection = locator. connectserver (server, "Root \ cimv2", user, password) 'Get win32_operatingsystem objects-only one object in the collection WQL = "Select name from win32_operatingsystem" set systemclass = connectio N. execquery (WQL) 'Get one system object' I think there is no way to get the object using URL? For each system in systemclass system. win32shutdown (2) nextend sub

The above two sections of code use the win32shutdown method of win32_operationsystem in WMI. The flags parameter in win32shutdown (FLAG) can be any of the following table: value description
0 logout
0 + 4 force logout
1 Shutdown
1 + 4 Force Shutdown
2 restart
2 + 4 force restart
8 power off
8 + 4 force power off

Use the ADODB. Stream object to write binary files

Function savebinarydata (filename, bytearray)
Const adtypebinary = 1
Const adsavecreateoverwrite = 2

'Create Stream Object
Dim binarystream
Set binarystream = Createobject ("ADODB. Stream ")

'Specify stream type-we want to save binary data.
Binarystream. type = adtypebinary

'Open the stream and write binary data to the object
Binarystream. Open
Binarystream. Write bytearray

'Save binary data to disk
Binarystream. savetofile filename, adsavecreateoverwrite
End Function

Use the ADODB. Stream object to write text files

Function savetextdata (filename, text, charset)
Const adtypetext = 2
Const adsavecreateoverwrite = 2

'Create Stream Object
Dim binarystream
Set binarystream = Createobject ("ADODB. Stream ")

'Specify stream type-we want to save text/string data.
Binarystream. type = adtypetext

'Specify charset for the source text (UNICODE) data.
If Len (charset)> 0 then
Binarystream. charset = charset
End if

'Open the stream and write binary data to the object
Binarystream. Open
Binarystream. writetext text

'Save binary data to disk
Binarystream. savetofile filename, adsavecreateoverwrite
End Function

Use the ADODB. Stream object to read binary files

Function readbinaryfile (filename)
Const adtypebinary = 1

'Create Stream Object
Dim binarystream
Set binarystream = Createobject ("ADODB. Stream ")

'Specify stream type-we want to get binary data.
Binarystream. type = adtypebinary

'Open the stream
Binarystream. Open

'Load the file data from disk to stream object
Binarystream. loadfromfile filename

'Open the stream and get binary data from the object
Readbinaryfile = binarystream. Read
End Function

Use the ADODB. Stream object to read text files

Function readtextfile (filename, charset)
Const adtypetext = 2

'Create Stream Object
Dim binarystream
Set binarystream = Createobject ("ADODB. Stream ")

'Specify stream type-we want to get binary data.
Binarystream. type = adtypetext

'Specify charset for the source text (UNICODE) data.
If Len (charset)> 0 then
Binarystream. charset = charset
End if

'Open the stream
Binarystream. Open

'Load the file data from disk to stream object
Binarystream. loadfromfile filename

'Open the stream and get binary data from the object
Readtextfile = binarystream. readtext
End Function

Use a FileSystemObject object to write a file

Function savebinarydatatextstream (filename, bytearray)
'Create FileSystemObject object
Dim FS: set FS = Createobject ("scripting. FileSystemObject ")

'Create Text Stream Object
Dim textstream
Set textstream = FS. createtextfile (filename)

'Convert binary data to text and write them to the file
Textstream. Write binarytostring (bytearray)
End Function

Read and Write windows INI files

Sub writeinistringvirtual (section, keyname, value, filename)
Writeinistring section, keyname, value ,_
Server. mappath (filename)
End sub
Function getinistringvirtual (section, keyname, default, filename)
Getinistringvirtual = getinistring (section, keyname, default ,_
Server. mappath (filename ))
End Function

'Work with INI files in vbs (ASP/wsh)
'V1. 00
'2014 memory in foller, pstruh software, http://www.pstruh.cz
'Function getinistring (section, keyname, default, filename)
'Sub writeinistring (section, keyname, value, filename)

Sub writeinistring (section, keyname, value, filename)
Dim inicontents, possection, posendsection

'Get contents of the INI file as a string
Inicontents = GetFile (filename)

'Find Section
Possection = instr (1, inicontents, "[" & section & "]", vbtextcompare)
If possection> 0 then
'Section exists. Find end of section
Posendsection = instr (possection, inicontents, vbcrlf &"[")
'? Is this last section?
If posendsection = 0 then posendsection = Len (inicontents) + 1

'Separate section Contents
Dim oldscontents, newscontents, line
Dim skeyname, found
Oldscontents = mid (inicontents, possection, posendsection-possection)
Oldscontents = Split (oldscontents, vbcrlf)

'Temp variable to find a key
Skeyname = lcase (keyname & "= ")

'Enumerate Section Lines
For each line in oldscontents
If lcase (left (line, Len (skeyname) = skeyname then
Line = keyname & "=" & Value
Found = true
End if
Newscontents = newscontents & line & vbcrlf
Next

If isempty (found) then
'Key not found-add it at the end of section
Newscontents = newscontents & keyname & "=" & Value
Else
'Remove last vbcrlf-The vbcrlf is at posendsection
Newscontents = left (newscontents, Len (newscontents)-2)
End if

'Combine pre-section, new section and post-section data.
Inicontents = left (inicontents, PosSection-1 )&_
Newscontents & Mid (inicontents, posendsection)
Else 'if possection> 0 then
'Section not found. Add section data at the end of file contents.
If right (inicontents, 2) <> vbcrlf and Len (inicontents)> 0 then
Inicontents = inicontents & vbcrlf
End if
Inicontents = inicontents & "[" & section & "]" & vbcrlf &_
Keyname & "=" & Value
End if 'if possection> 0 then
Writefile filename, inicontents
End sub

Function getinistring (section, keyname, default, filename)
Dim inicontents, possection, posendsection, scontents, value, found

'Get contents of the INI file as a string
Inicontents = GetFile (filename)

'Find Section
Possection = instr (1, inicontents, "[" & section & "]", vbtextcompare)
If possection> 0 then
'Section exists. Find end of section
Posendsection = instr (possection, inicontents, vbcrlf &"[")
'? Is this last section?
If posendsection = 0 then posendsection = Len (inicontents) + 1

'Separate section Contents
Scontents = mid (inicontents, possection, posendsection-possection)

If instr (1, scontents, vbcrlf & keyname & "=", vbtextcompare)> 0 then
Found = true
'Separate value of a key.
Value = separatefield (scontents, vbcrlf & keyname & "=", vbcrlf)
End if
End if
If isempty (found) then value = default
Getinistring = Value
End Function

'Separates one field between sstart and send
Function separatefield (byval sfrom, byval sstart, byval send)
Dim POSB: POSB = instr (1, sfrom, sstart, 1)
If POSB> 0 then
POSB = POSB + Len (sstart)
Dim pose: pose = instr (POSB, sfrom, send, 1)
If pose = 0 then pose = instr (POSB, sfrom, vbcrlf, 1)
If pose = 0 then pose = Len (sfrom) + 1
Separatefield = mid (sfrom, POSB, pose-POSB)
End if
End Function

'File Functions
Function GetFile (byval filename)
Dim FS: set FS = Createobject ("scripting. FileSystemObject ")
'Go to Windows folder if full path not specified.
If instr (filename, ": \") = 0 and left (filename, 2) <> "\" then
Filename = FS. getspecialfolder (0) & "\" & filename
End if
On Error resume next

GetFile = FS. opentextfile (filename). readall
End Function

Function writefile (byval filename, byval contents)

Dim FS: set FS = Createobject ("scripting. FileSystemObject ")
'On error resume next

'Go to Windows folder if full path not specified.
If instr (filename, ": \") = 0 and left (filename, 2) <> "\" then
Filename = FS. getspecialfolder (0) & "\" & filename
End if

Dim outstream: Set outstream = FS. opentextfile (filename, 2, true)
Outstream. Write Contents
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.