Sunday, February 10, 2008

VBscript Reference

Hi, this weekend i was exploring my folder and i found a file very interesting...
People can use to develomp in VBscript, This is a litle manual of develomp Vbscript

Display Output:

  • DISPLAY TO STANDARD OUTPUT
    Wscript.Echo “Display this text”
  • DISPLAY TO MESSAGE BOX
    MsgBox(“Prompt”, vbOKCancel, “Title”)
  • DISPLAY TO POPUP DIALOG BOX
    WshShell.Popup(“Message”, 5, “Title”, 1)
    5: number of seconds to display popup box
Error Handling:

  • IGNORE RUNTIME ERRORS
    On Error Resume Next
  • FORCE VARIABLE DECLARATION
    Option Explicit
  • CHECK FOR AN ERROR
    If Err.Number Then
    ‘ an error occurred
    End If
  • CLEAR THE ERROR CACHE
    Err.Clear
    (execute this statement each time you check the Err object)

WMI:

  • 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
    Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service")

ADSI:

  • COMPUTER VARIABLE (local computer)
    strComputer = “localhost”
  • RETRIEVE AN OU
    Set objOU = GetObject("LDAP://ou=finance,dc=fabrikam,dc=com")
  • RETRIEVE A USER ACCOUNT~
    Set objUser = GetObject("LDAP://cn=ken myer, ou=Finance, dc=fabrikam,
  • BIND TO THE LOCAL COMPUTER
    Set colAccounts = GetObject("WinNT://" & strComputer)

FileSystemObject:

  • OPEN TEXT FILE FOR READING
    Const ForReading = 1

    Set objFSO = CreateObject _
    ("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
    ("c:\scripts\servers.txt", ForReading)
  • OPEN TEXT FILE FOR WRITING
    Const ForWriting = 2

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

Search Active Directory:

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


Microsoft Office:

  • 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
    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")

VbScript Syntax

  • CONDITIONAL STATEMENTS
    If Then

    If x = 4 Then

    ElseIf x = 5 Then

    Else
    ...
    End If


    Select Case

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

    End Select

  • ARRAYS
    arrItems = Array("a","b","c")

    Dim arr(2)
    arr(0) = 20
    arr(1) = 30

    ReDim Preserve arr(3)
    arr(2) = 40

  • LOOPS
    For Loops

    For Each x in arr
    ...
    Next

    For i = 1 to 10
    ...
    Next
  • While Loops

    While x < 5 …
    Wend
  • Do Loops

    Do Until x > 5
    ...
    Loop

    Do While x <> 5

    Do
    ...
    Loop While x <5

  • FUNCTIONS AND SUBROUTINES
  • Function

    Function TestFunc

    TestFunc = 10
    End Function
  • Subroutine

    Sub TestSub

    End Sub

Run Scripts:

  • SEND OUTPUT TO COMMAND WINDOW
    C:\> cscript test.vbs
  • SET DEFAULT TO CSCRIPT
    C:\> cscript //H:cscript
  • SEND OUTPUT TO MESSAGE BOX
    C:\> wscript test.vbs
  • SET DEFAULT TO WSCRIPT
    C:\> cscript //H:wscript
    Post a Comment