具体用法:新建一个txt文档,将脚本保存在其中,然后将文件类型保存为.VBS,点击运行。
Adding Elements to a Dictionary
Demonstration scrīpt that adds three key-item pairs to a scrīpt Runtime Dictionary. scrīpt must be run on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")
objDictionary.Add "Printer 1", "Printing"
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
Creating an Instance of Internet Explorer
Demonstration scrīpt that creates an instance of Internet Explorer, opened to a blank page.
Set ōbjExplorer = Wscrīpt.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width=300
objExplorer.Height = 150
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Visible = 1
Creating scrīpt Documentation Using scrīpt Comments
Demonstrates the use of the FileSystemObject as a way to copy comments from a scrīpt to a separate text file. Requires comments to have been marked using '*.
Const ForReading = 1
Const ForWriting = 2
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjscrīptFile = objFSO.OpenTextFile("c:\scrīpts\Service_Monitor.vbs", _
ForReading)
Set ōbjCommentFile = objFSO.OpenTextFile("c:\scrīpts\Comments.txt", _
ForWriting, TRUE)
Do While objscrīptFile.AtEndOfStream <> TRUE
strCurrentLine = objscrīptFile.ReadLine
intIsComment = Instr(1,strCurrentLine,"'*")
If intIsComment > 0 Then
objCommentFile.Write strCurrentLine & VbCrLf
End If
Loop
objscrīptFile.Close
objCommentFile.Close
Determining the Number of Items in a Dictionary
Demonstration scrīpt that counts the number of key-item pairs in a scrīpt Runtime Dictionary. scrīpt must be run on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")
objDictionary.Add "Printer 1", "Printing"
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
Wscrīpt.Echo objDictionary.Count
Displaying Real Time Events in a Command Window
Creates a temporary event consumer that monitors the event log for error events. When an error event occurs, the scrīpt displays the event information in the command window.
strComputer = "."
Set ōbjWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Security)}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("Select * from __InstanceCreationEvent within 5 where TargetInstance isa " _
& "'Win32_NTLogEvent' and TargetInstance.EventType = '1'")
Do
Set ōbjLatestEvent = colMonitoredEvents.NextEvent
Wscrīpt.Echo "Record No.: " & _
objLatestEvent.TargetInstance.RecordNumber
Wscrīpt.Echo "Event ID: " & objLatestEvent.TargetInstance.EventCode
Wscrīpt.Echo "Time: " & objLatestEvent.TargetInstance.TimeWritten
Wscrīpt.Echo "Source: " & objLatestEvent.TargetInstance.SourceName
Wscrīpt.Echo "Category: " & _
objLatestEvent.TargetInstance.CategoryString
Wscrīpt.Echo "Event Type: " & objLatestEvent.TargetInstance.Type
Wscrīpt.Echo "Computer: " & _
objLatestEvent.TargetInstance.ComputerName
Wscrīpt.Echo "User: " & objLatestEvent.TargetInstance.User
Wscrīpt.echo "Text: " & objLatestEvent.TargetInstance.Message
Loop
Displaying Tabular Output in a Command Window
Retrieves service data from a computer, and then outputs that data in tabular format in a command window.
Set colServices = GetObject("winmgmts:"). _
ExecQuery("Select * from Win32_Service")
For Each objService in colServices
intPadding = 50 - Len(objService.DisplayName)
intPadding2 = 17 - Len(objService.StartMode)
strDisplayName = objService.DisplayName & Space(intPadding)
strStartMode = objService.StartMode & Space(intPadding2)
Wscrīpt.Echo strDisplayName & strStartMode & objService.State
Next
Masking Command Line Passwords
Demonstration scrīpt that uses scrīptPW.dll to mask passwords entered at the command line.
Set ōbjPassword = CreateObject("scrīptPW.Password")
Wscrīpt.StdOut.Write "Please enter your password:"
strPassword = objPassword.GetPassword()
Wscrīpt.Echo
Wscrīpt.Echo "Your password is: " & strPassword
Masking Passwords Using Internet Explorer
Demonstration scrīpt that creates an instance of Internet Explorer, and retrieves a password typed into a password-style text box. Requires a Web page named password.htm with the appropriate text box.
Set ōbjExplorer = Wscrīpt.CreateObject _
("InternetExplorer.Application", "IE_")
objExplorer.Navigate "file:///c:\scrīpts\password.htm"
objExplorer.Visible = 1
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width=400
objExplorer.Height = 250
objExplorer.Left = 0
objExplorer.Top = 0
Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
Wscrīpt.Sleep 250
Loop
strPassword = objExplorer.Document.Body.All.PasswordBox.Value
objExplorer.Quit
Wscrīpt.Sleep 250
Wscrīpt.Echo strPassword
Removing All Elements from a Dictionary
Demonstration scrīpt that deletes all the key-item pairs from a scrīpt Runtime Dictionary. scrīpt must be run on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")
objDictionary.Add "Printer 1", "Printing"
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
colKeys = objDictionary.Keys
Wscrīpt.Echo "First run: "
For Each strKey in colKeys
Wscrīpt.Echo strKey
Next
objDictionary.RemoveAll
colKeys = objDictionary.Keys
Wscrīpt.Echo VbCrLf & "Second run: "
For Each strKey in colKeys
Wscrīpt.Echo strKey
Next
Removing Debugging Comments
Demonstrates the use of the FileSystemObject as a way to remove debugging comments from a scrīpt. Requires comments to have been marked as '* BUG.
Const ForReading = 1
Const ForWriting = 2
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjTextFile = objFSO.OpenTextFile("C:\scrīpts\CreateUser.vbs", ForReading)
Do While objTextFile.AtEndOfStream <> true
strNextLine = objTextFile.Readline
intCheckForBugComment = Instr(strNextLine, "'* BUG")
If intCheckForBugComment = 0 Then
strSavedLines = strSavedLines & strNextLine & VbCrLf
End If
Loop
Set ōbjTextFile = objFSO.OpenTextFile _
("c:\scrīpts\CreateUser.vbs ", ForWriting)
objTextFile.Write strSavedLines
objTextFile.Close
Removing One Element from a Dictionary
Demonstration scrīpt that deletes a specific key-item pair from a scrīpt Runtime Dictionary. scrīpt must be run on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")
objDictionary.Add "Printer 1", "Printing"
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
colKeys = objDictionary.Keys
Wscrīpt.Echo "First run: "
For Each strKey in colKeys
Wscrīpt.Echo strKey
Next
objDictionary.Remove("Printer 2")
colKeys = objDictionary.Keys
Wscrīpt.Echo VbCrLf & "Second run: "
For Each strKey in colKeys
Wscrīpt.Echo strKey
Next
Retrieving Command Line Arguments from an Active Directory Container
Demonstration scrīpt that retrieves the names of all the computers in an Active Directory container, and then returns service information from each of those computers.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")
i = 0
Set ōbjOU = GetObject("LDAP://CN=Computers, DC=fabrikam, DC=com")
objOU.Filter = Array("Computer")
For Each objComputer in objOU
objDictionary.Add i, objComputer.CN
i = i + 1
Next
For Each objItem in objDictionary
Set colServices = GetObject("winmgmts://" & _
objDictionary.Item(objItem) _
& "").ExecQuery("Select * from Win32_Service")
Wscrīpt.Echo colServices.Count
Next
Retrieving Command Line Arguments from a Text File
Demonstration scrīpt that opens a hypothetical text file consisting of server names, then retrieves service information from each on the servers in the file.
Const ForReading = 1
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjTextFile = objFSO.OpenTextFile("c:\scrīpts\servers.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
Set colServices = GetObject("winmgmts://" & _
objDictionary.Item(objItem) _
& "").ExecQuery("Select * from Win32_Service")
Wscrīpt.Echo colServices.Count
Next
Retrieving a Web Page
Retrieves the HTML source for the Web page http://www.microsoft.com. This scrīpt contributed by Maxim Stepin of Microsoft.
url="http://www.microsoft.com"
Set ōbjHTTP = CreateObject("MSXML2.XMLHTTP")
Call objHTTP.Open("GET", url, FALSE)
objHTTP.Send
Wscrīpt.Echo(objHTTP.ResponseText)
Saving Data in XML Format
Demonstration scrīpt that retrieves service information for a computer, and then saves that data as an XML file.
Const ForAppending = 2
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjTextFile = objFSO.OpenTextFile _
("c:\scrīpts\service_status.xml", ForAppending, True)
objTextFile.WriteLine ""
objTextFile.Write ""
objTextFile.WriteLine ""
strComputer = "."
Set colServices = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"). _
ExecQuery("Select * from Win32_Service")
For Each objService in colServices
objTextFile.WriteLine ""
objTextFile.WriteLine ""
objTextFile.WriteLine objService.DisplayName
objTextFile.WriteLine ""
objTextFile.WriteLine ""
objTextFile.WriteLine objService.State
objTextFile.WriteLine ""
objTextFile.WriteLine ""
Next
objTextFile.WriteLine ""
objTextFile.Close
Sorting WMI Data
Demonstration scrīpt showing how WMI data can be sorted using a disconnected recordset (by itself, WMI does not allow you to specify a sort order for returned data). In this scrīpt, service information is retrieved using WMI and is stored in a disconnected recordset, a recordset that is not tied to a physical data source. The Sort method is then used to sort the service data by service state rather than by service name.
Const adVarChar = 200
Const MaxCharacters = 255
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "ServiceName", adVarChar, MaxCharacters
DataList.Fields.Append "ServiceState", adVarChar, MaxCharacters
DataList.Open
strComputer = "."
Set ōbjWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set ServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service")
For Each Service in ServiceList
DataList.AddNew
DataList("ServiceName") = Service.Name
DataList("ServiceState") = Service.State
DataList.Update
Next
DataList.Sort = "ServiceState"
DataList.MoveFirst
Do Until DataList.EOF
Wscrīpt.Echo DataList.Fields.Item("ServiceName") _
& vbTab & DataList.Fields.Item("ServiceState")
DataList.MoveNext
Loop
Suppressing Multiple Event Notifications
Issues an alert if available space on a disk drive falls below 100 megabytes. Will wait one hour before issuing the next alert.
dtmStartTime = Now
strComputer = "."
Set ōbjWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set ōbjDiskDrives = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each objDrive in objDiskDrives
If objDrive.FreeSpace < 10000000 Then
Wscrīpt.Echo "Drive is low on disk space."
End If
Next
Do
Set ōbjDiskDrives = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each objDrive in objDiskDrives
If objDrive.FreeSpace < 10000000 Then
intElapsedHours = DateDiff("h", dtmStartTime, Now)
If intElapsedHours >= 1 Then
Wscrīpt.Echo "Drive is low on disk space."
dtmStartTime = Now
End If
End If
Next
Wscrīpt.Sleep 1000
Loop
Tracking scrīpt Progress in a Command Window
Demonstrates the use of StdOut as a method for indicating the progress being made by a scrīpt.
Wscrīpt.Echo "Processing information. This might take several minutes."
strComputer = "."
Set colServices = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"). _
ExecQuery("Select * from Win32_Service")
For Each objService in colServices
Wscrīpt.StdOut.Write(".")
Next
Wscrīpt.StdOut.WriteLine
Wscrīpt.Echo "Service information processed."
Tracking scrīpt Progress Using Internet Explorer
Demonstrates how to use Internet Explorer as a method for indicating the progress being made by a scrīpt.
Set ōbjExplorer = Wscrīpt.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width=400
objExplorer.Height = 200
objExplorer.Left = 0
objExplorer.Top = 0
Do While (objExplorer.Busy)
Wscrīpt.Sleep 200
Loop
objExplorer.Visible = 1
objExplorer.Document.Body.InnerHTML = "Retrieving service information. " _
& "This might take several minutes to complete."
strComputer = "."
Set colServices = GetObject("winmgmts: \\" & strComputer & "\root\cimv2"). _
ExecQuery("Select * from Win32_Service")
For Each objService in colServices
Wscrīpt.Sleep 200
Next
objExplorer.Document.Body.InnerHTML = "Service information retrieved."
Wscrīpt.Sleep 3000
Wscrīpt.Quit
Using a Text File as a Command Line Argument
Demonstration scrīpt that allows you to drag a text file (consisting of server names) onto the scrīpt icon in
Windows Explorer. The scrīpt then opens the text file, then retrieves service information from each on the servers in the file.
Set ōbjArgs = Wscrīpt.Arguments
Const ForReading = 1
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjTextFile = objFSO.OpenTextFile(objArgs(0), ForReading)
i = 0
Do While objTextFile.AtEndOfStream <> True
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
Set colServices = GetObject("winmgmts://" & objDictionary.Item(objItem) _
& "").ExecQuery("Select * from Win32_Service")
Wscrīpt.Echo colServices.Count
Next
Verifying the Existence of a Dictionary Key
Demonstration scrīpt that verifies the existence of a particular key within a scrīpt Runtime Dictionary. scrīpt must be run on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")
objDictionary.Add "Printer 1", "Printing"
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
If objDictionary.Exists("Printer 4") Then
Wscrīpt.Echo "Printer 4 is in the Dictionary."
Else
Wscrīpt.Echo "Printer 4 is not in the Dictionary."
End If