Vous êtes sur la page 1sur 3

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>
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 ... ...
If Then Select Case Next Loop
Const ADS_SCOPE_ONELEVEL = 1

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
... … Loop Until x > 5
objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL
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
Function TestFunc Sub TestSub
http://www.microsoft.com arr(1) = 30
… …
/technet/scriptcenter/guide TestFunc = 10 End Sub
EXCEL 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:\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
Filename: B3FE7EC.doc
Directory: C:\Documents and Settings\Scott Sweeney\Local Settings\Temporary
Internet Files\Content.MSO
Template: C:\Documents and Settings\Scott Sweeney\Application
Data\Microsoft\Templates\Normal.dot
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.)

Vous aimerez peut-être aussi