VBScript Quick Reference

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

VBScript Quick Reference

VBScript Quick Reference

DISPLAY TO STANDARD OUTPUT

HTA SECTION <head> <title>HTA Test</title> <HTA:APPLICATION APPLICATIONNAME="HTA Test" SCROLL="yes" SINGLEINSTANCE="yes" WINDOWSTATE="maximize" > </head> </head> SCRIPT SECTION <script language="VBScript> Sub window_OnLoad ' Script to run on startup End Sub Sub TestSub ' Script code goes here End Sub </script> HTML SECTION <body> <input type="button" value="Run Script" name="run_button" onClick="TestSub"> </body>

IGNORE RUNTIME ERRORS

FORCE VARIABLE DECLARATION

Wscript.Echo Display this text


DISPLAY TO MESSAGE BOX

On Error Resume Next


CHECK FOR AN ERROR

Option Explicit
CLEAR THE ERROR CACHE

MsgBox(Prompt, vbOKCancel, Title)


DISPLAY TO POPUP DIALOG BOX

If Err.Number Then an error occurred End If

Err.Clear (execute this statement each time you check the Err object)

WshShell.Popup(Message, 5, Title, 1)
5: number of seconds to display popup box 1: displays the OK and Cancel buttons

COMPUTER VARIABLE (local computer)

strComputer = .
CONNECT TO WMI

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")


QUERY: RETRIEVE ALL PROCESSES

Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process") ")


QUERY: RETRIEVE ALL SERVICES

OPEN TEXT FILE FOR READING

Const ForReading = 1 Set objFSO = CreateObject _ ("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c:\scripts\servers.txt", ForReading)
CREATE DICTONARY OBJECT OPEN TEXT FILE FOR WRITING

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

COMPUTER VARIABLE (local computer)

strComputer = localhost
RETRIEVE AN OU

Const ForWriting = 2 Set objFSO = CreateObject _ ("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c:\scripts\servers.txt", ForWriting)

Set objDictionary = _ CreateObject("Scripting.Dictionary")

Set objOU = GetObject("LDAP://ou=finance,dc=fabrikam,dc=com")


RETRIEVE A USER ACCOUNT

POPULATE DICTIONARY OBJECT

Set objUser = GetObject("LDAP://cn=ken myer, ou=Finance, dc=fabrikam, dc=com")


BIND TO THE LOCAL COMPUTER

objDictionary.Add key, item

Set colAccounts = GetObject("WinNT://" & strComputer)

VBScript Quick Reference

VBScript Quick Reference

LOOPS

For Loops
On Error Resume Next Const ADS_SCOPE_ONELEVEL = 1 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConnection objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL objCommand.CommandText = _ "SELECT Name FROM 'LDAP://OU=finance,dc=fabrikam,dc=com' Set objRecordSet = objCommand.Execute CONDITIONAL STATEMENTS

Do Loops Do Until x > 5 ... Loop Do While x < 5 ... Loop Do ... Loop Until x > 5 Do ... Loop While x < 5

If Then If x = 4 Then ElseIf x = 5 Then Else ... End If Learn more about scripting from the Microsoft Windows 2000 Scripting Guide, available online (and yes, despite the title most of the concepts apply to later versions of Windows too): http://www.microsoft.com /technet/scriptcenter/guide

Select Case Select Case x Case 1 ... Case 2 ... Case Else End Select

For Each x in arr ... Next For i = 1 to 10 ... Next

While Loops While x < 5 Wend

ARRAYS

arrItems = Array("a","b","c") Dim arr(2) arr(0) = 20 arr(1) = 30 ReDim Preserve arr(3) arr(2) = 40 Function

FUNCTIONS AND SUBROUTINES

Subroutine Sub TestSub End Sub

EXCEL Set objExcel = CreateObject("Excel.Application") 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") ACCESS Set objAccess = CreateObject("Access.Application") objAccess.OpenCurrentDatabase "C:\Scripts\Test.mdb" OUTLOOK

Function TestFunc TestFunc = 10 End Function

SEND OUTPUT TO COMMAND WINDOW

SET DEFAULT TO CSCRIPT

C:\> cscript test.vbs

C:\> cscript //H:cscript

SEND OUTPUT TO MESSAGE BOX

SET DEFAULT TO WSCRIPT

OUTLOOK

Set objOutlook = CreateObject("Outlook.Application") Set objNamespace = objOutlook.GetNamespace("MAPI")

C:\> wscript test.vbs

C:\> cscript //H:wscript

You might also like