VBScript Quick Reference VBScript Quick Reference
IGNORE RUNTIME ERRORS FORCE VARIABLE DECLARATION
DISPLAY TO STANDARD OUTPUT HTA SECTION
<head> On Error Resume Next Option Explicit
[Link] “Display this text” <title>HTA Test</title>
<HTA:APPLICATION
APPLICATIONNAME="HTA Test" CHECK FOR AN ERROR CLEAR THE ERROR CACHE
DISPLAY TO MESSAGE BOX SCROLL="yes" SINGLEINSTANCE="yes" If [Link] Then [Link]
WINDOWSTATE="maximize" >
MsgBox(“Prompt”, vbOKCancel, “Title”) ‘ an error occurred (execute this statement each
</head>
End If time you check the Err object)
DISPLAY TO POPUP DIALOG BOX
SCRIPT SECTION
[Link](“Message”, 5, “Title”, 1) <script language="VBScript“>
5: number of seconds to display popup box Sub window_OnLoad COMPUTER VARIABLE (local computer)
1: displays the OK and Cancel buttons ' Script to run on startup
strComputer = “.”
End Sub
Sub TestSub
' Script code goes here
CONNECT TO WMI
End Sub
</script> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
HTML SECTION
QUERY: RETRIEVE ALL PROCESSES
<body> <input type="button"
value="Run Script" Set colProcessList = [Link]("Select * from Win32_Process")
name="run_button" ")
> </body> QUERY: RETRIEVE ALL SERVICES
OPEN TEXT FILE FOR READING
Const ForReading = 1 Set colServiceList = [Link]("Select * from Win32_Service")
Set objFSO = CreateObject _
("[Link]")
Set objTextFile = [Link] _ COMPUTER VARIABLE (local computer)
("c:\scripts\[Link]", ForReading) strComputer = “localhost”
CREATE DICTONARY OBJECT RETRIEVE AN OU
OPEN TEXT FILE FOR WRITING Set objDictionary = _ Set objOU = GetObject("LDAP://ou=finance,dc=fabrikam,dc=com")
Const ForWriting = 2 CreateObject("[Link]")
RETRIEVE A USER ACCOUNT
Set objFSO = CreateObject _
("[Link]") Set objUser = GetObject("LDAP://cn=ken myer, ou=Finance, dc=fabrikam, dc=com")
POPULATE DICTIONARY OBJECT
Set objTextFile = [Link] _
("c:\scripts\[Link]", ForWriting) [Link] key, item BIND TO THE LOCAL COMPUTER
Set colAccounts = GetObject("WinNT://" & strComputer)
VBScript Quick Reference VBScript Quick Reference
LOOPS
For Loops Do Loops
For Each x in arr Do Until x > 5
On Error Resume Next CONDITIONAL STATEMENTS ... ...
If Then Select Case Next Loop
Const ADS_SCOPE_ONELEVEL = 1
Set objConnection = CreateObject("[Link]") If x = 4 Then Select Case x For i = 1 to 10 Do While x < 5
Set objCommand = CreateObject("[Link]") … Case 1 ... ...
[Link] = "ADsDSOObject" ElseIf x = 5 ... Next Loop
[Link] "Active Directory Provider"
Then Case 2
Set [Link] = objConnection Do
… ...
Else Case Else While Loops ...
[Link]("Page Size") = 1000
... … Loop Until x > 5
[Link]("Searchscope") = ADS_SCOPE_ONELEVEL
End If End Select While x < 5
[Link] = _ … Do
"SELECT Name FROM 'LDAP://OU=finance,dc=fabrikam,dc=com'“ Wend ...
Set objRecordSet = [Link] Learn more about scripting Loop While x < 5
from the Microsoft
Windows 2000 Scripting ARRAYS
Guide, available online
(and yes, despite the title arrItems =
most of the concepts apply Array("a","b","c") FUNCTIONS AND SUBROUTINES
to later versions of Function Subroutine
Windows too): Dim arr(2)
arr(0) = 20
Function TestFunc Sub TestSub
[Link] arr(1) = 30
… …
/technet/scriptcenter/guide TestFunc = 10 End Sub
EXCEL ReDim Preserve arr(3)
End Function
Set objExcel = CreateObject("[Link]") arr(2) = 40
[Link] = True
Set objWorkbook = [Link]
WORD
Set objWord = CreateObject("[Link]")
[Link] = True
Set objDoc = [Link]("c:\scripts\[Link]")
SEND OUTPUT TO COMMAND WINDOW SET DEFAULT TO CSCRIPT
ACCESS
Set objAccess = CreateObject("[Link]") C:\> cscript [Link] C:\> cscript //H:cscript
[Link] "C:\Scripts\[Link]"
OUTLOOK SEND OUTPUT TO MESSAGE BOX SET DEFAULT TO WSCRIPT
Set objOutlook = CreateObject("[Link]")
Set objNamespace = [Link]("MAPI")
C:\> wscript [Link] C:\> cscript //H:wscript
Filename: [Link]
Directory: C:\Documents and Settings\Scott Sweeney\Local Settings\Temporary
Internet Files\[Link]
Template: C:\Documents and Settings\Scott Sweeney\Application
Data\Microsoft\Templates\[Link]
Title:
Subject:
Author: Greg Stemp
Keywords:
Comments:
Creation Date: 5/30/2007 8:03:00 PM
Change Number: 27
Last Saved On: 5/31/2007 10:12:00 AM
Last Saved By: Greg Stemp
Total Editing Time: 178 Minutes
Last Printed On: 1/22/2008 9:43:00 PM
As of Last Complete Printing
Number of Pages: 2
Number of Words: 18 (approx.)
Number of Characters: 107 (approx.)