You are on page 1of 2

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
Wscript.Echo “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 Err.Number Then Err.Clear
WINDOWSTATE="maximize" >
MsgBox(“Prompt”, vbOKCancel, “Title”) ‘ an error occurred (execute this statement each
</head>
</head> End If time you check the Err object)

DISPLAY TO POPUP DIALOG BOX


SCRIPT SECTION
WshShell.Popup(“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 = objWMIService.ExecQuery("Select * from Win32_Process")
name="run_button" ")
onClick="TestSub">
</body> QUERY: RETRIEVE ALL SERVICES
OPEN TEXT FILE FOR READING

Const ForReading = 1 Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service")

Set objFSO = CreateObject _


("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _ COMPUTER VARIABLE (local computer)
("c:\scripts\servers.txt", 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("Scripting.Dictionary")
RETRIEVE A USER ACCOUNT
Set objFSO = CreateObject _
("Scripting.FileSystemObject") Set objUser = GetObject("LDAP://cn=ken myer, ou=Finance, dc=fabrikam, dc=com")
POPULATE DICTIONARY OBJECT
Set objTextFile = objFSO.OpenTextFile _
("c:\scripts\servers.txt", ForWriting) objDictionary.Add 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 ... ...
Const ADS_SCOPE_ONELEVEL = 1 If Then Select Case Next Loop

Set objConnection = CreateObject("ADODB.Connection") If x = 4 Then Select Case x For i = 1 to 10 Do While x < 5


Set objCommand = CreateObject("ADODB.Command") … Case 1 ... ...
objConnection.Provider = "ADsDSOObject" ElseIf x = 5 ... Next Loop
objConnection.Open "Active Directory Provider"
Then Case 2
Set objCommand.ActiveConnection = objConnection Do
… ...
Else Case Else While Loops ...
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL ... … Loop Until x > 5
End If End Select While x < 5
objCommand.CommandText = _ … Do
"SELECT Name FROM 'LDAP://OU=finance,dc=fabrikam,dc=com'“ Wend ...
Set objRecordSet = objCommand.Execute 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
arr(1) = 30 Function TestFunc Sub TestSub
http://www.microsoft.com … …
/technet/scriptcenter/guide TestFunc = 10 End Sub
EXCEL ReDim Preserve arr(3)
Set objExcel = CreateObject("Excel.Application") arr(2) = 40 End Function
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add

WORD
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("c:\scripts\test.doc")

SEND OUTPUT TO COMMAND WINDOW SET DEFAULT TO CSCRIPT


ACCESS
Set objAccess = CreateObject("Access.Application") C:\> cscript test.vbs C:\> cscript //H:cscript
objAccess.OpenCurrentDatabase "C:\Scripts\Test.mdb"

OUTLOOK SEND OUTPUT TO MESSAGE BOX SET DEFAULT TO WSCRIPT


Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
C:\> wscript test.vbs C:\> cscript //H:wscript
OUTLOOK

You might also like