Vbscript reference


Published on

Published in: Education
  • Be the first to comment

Vbscript reference

  1. 1. 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 CHECK FOR AN ERROR CLEAR THE ERROR CACHE DISPLAY TO MESSAGE BOX APPLICATIONNAME="HTA Test" 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 SECTIONWshShell.Popup(“Message”, 5, “Title”, 1) <script language="VBScript“> COMPUTER VARIABLE (local computer)5: number of seconds to display popup box Sub window_OnLoad1: 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 & "rootcimv2") 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"> OPEN TEXT FILE FOR READING </body> QUERY: RETRIEVE ALL SERVICESConst ForReading = 1 Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service")Set objFSO = CreateObject _ ("Scripting.FileSystemObject")Set objTextFile = objFSO.OpenTextFile _ COMPUTER VARIABLE (local computer) ("c:scriptsservers.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 ACCOUNTSet objFSO = CreateObject _ ("Scripting.FileSystemObject") POPULATE DICTIONARY OBJECT Set objUser = GetObject("LDAP://cn=ken myer, ou=Finance, dc=fabrikam, dc=com")Set objTextFile = objFSO.OpenTextFile _ ("c:scriptsservers.txt", ForWriting) objDictionary.Add key, item BIND TO THE LOCAL COMPUTER Set colAccounts = GetObject("WinNT://" & strComputer)
  2. 2. 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 If x = 4 Then Select Case x For i = 1 to 10 Do While x < 5 Set objConnection = CreateObject("ADODB.Connection") 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 Loop Until x > 5 objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL ... … End If End Select While x < 5 … Do objCommand.CommandText = _ "SELECT Name FROM LDAP://OU=finance,dc=fabrikam,dc=com“ Wend ... Learn more about scripting Loop While x < 5 Set objRecordSet = objCommand.Execute from the Microsoft Windows 2000 Scripting Guide, ARRAYS available online (and yes, despite the title most of the arrItems = concepts apply to later Array("a","b","c") FUNCTIONS AND SUBROUTINES versions of Windows too): Function Subroutine Dim arr(2) http://www.microsoft.com arr(0) = 20 Function TestFunc Sub TestSub /technet/scriptcenter/guide arr(1) = 30 … … EXCEL TestFunc = 10 End Sub ReDim Preserve arr(3) End Function Set objExcel = CreateObject("Excel.Application") arr(2) = 40 objExcel.Visible = True Set objWorkbook = objExcel.Workbooks.Add WORD Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Open("c:scriptstest.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:ScriptsTest.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:wscriptOUTLOOK