Friday, August 7, 2009

Useful VB Script

4 comments:

  1. WMI Samples - Connect to remote WMI computers
    ===============================================
    List all shares on local computer using
    credentials of currently logged on user
    -----------------------------------------------
    Option Explicit

    ListShares()
    WScript.Echo vbCrlf & "Ready."

    Sub ListShares()
    Dim strObject
    Dim colShares
    Dim objWMIService, objShare

    Set objWMIService = GetObject( "winmgmts:" )
    Set colShares = objWMIService.ExecQuery( "Select * from Win32_Share" )
    For Each objShare In colShares
    Wscript.Echo objShare.Name & " [" & objShare.Path & "]"
    Next
    End Sub

    ReplyDelete
  2. List all shares on local or remote computer using credentials of currently logged on user
    ------------------------------------------------
    Option Explicit

    Dim strComputer
    Do
    strComputer = inputbox( "Please enter name of a computer (or . for local host)", "Input" )
    Loop until strComputer <> ""
    ListShares( strComputer )
    WScript.Echo vbCrlf & "Ready."

    Sub ListShares( strComputer )
    Dim strObject
    Dim colShares
    Dim objWMIService, objShare

    Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
    Set colShares = objWMIService.ExecQuery( "Select * from Win32_Share" )
    For Each objShare In colShares
    Wscript.Echo objShare.Name & " [" & objShare.Path & "]"
    Next
    End Sub

    ReplyDelete
  3. Impersonation sample: List all shares on a remote computer using different credentials than logged on user
    ----------------------------------------------
    Option Explicit

    Dim strComputer, strUser, strPassword
    Do
    strComputer = inputbox( "Please enter computername (or . for local host)", "Input" )
    Loop until strComputer <> ""
    Do
    strUser = inputbox( "Please enter username", "Input" )
    Loop until strUser <> ""
    Do
    strPassword = inputbox( "Please enter password", "Input" )
    Loop until strPassword <> ""
    ListShares strComputer, strUser, strPassword

    WScript.Echo vbCrlf & "Ready."


    Sub ListShares( strComputer, strUser, strPassword )
    Dim strObject
    Dim objLocator, objWMIService, objShare
    Dim colShares

    Set objLocator = CreateObject( "WbemScripting.SWbemLocator" )
    Set objWMIService = objLocator.ConnectServer ( strComputer, "root/cimv2", strUser, strPassword )
    objWMIService.Security_.impersonationlevel = 3
    Set colShares = objWMIService.ExecQuery( "Select * from Win32_Share" )
    For Each objShare In colShares
    Wscript.Echo objShare.Name & " [" & objShare.Path & "]"
    Next
    End Sub

    ReplyDelete
  4. WMI Samples - Exchange
    ================================================
    MSExchangeAL Performance Counter
    ------------------------------------------------
    The Recipient Update Service (RUS) plays a crucial role in the day-to-day operations of Exchange 2000 because it is responsible for keeping e-mail addresses and membership of address lists up to date. You should measure the Address List Queue Length when examining the RUS:
    Address List Queue Length - shows the load the Recipient Update Service is under. If this value is consistently high compared to your baseline, you should seriously consider upgrading the server that has this role, or transferring the role from a weak or overloaded server to a more powerful one.

    On Error Resume Next
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_MSExchangeAL_MSExchangeAL",,48)
    For Each objItem in colItems
    Wscript.Echo "ActiveAddressListThreads: " & objItem.ActiveAddressListThreads
    Wscript.Echo "ActiveRecipientThreads: " & objItem.ActiveRecipientThreads
    Wscript.Echo "AddressListModifications: " & objItem.AddressListModifications
    Wscript.Echo "AddressListsQueueLength: " & objItem.AddressListsQueueLength
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "Frequency_Object: " & objItem.Frequency_Object
    Wscript.Echo "Frequency_PerfTime: " & objItem.Frequency_PerfTime
    Wscript.Echo "Frequency_Sys100NS: " & objItem.Frequency_Sys100NS
    Wscript.Echo "LDAPModifycalls: " & objItem.LDAPModifycalls
    Wscript.Echo "LDAPModifycallsPersec: " & objItem.LDAPModifycallsPersec
    Wscript.Echo "LDAPModifyfailures: " & objItem.LDAPModifyfailures
    Wscript.Echo "LDAPModifyfailuresPersec: " & objItem.LDAPModifyfailuresPersec
    Wscript.Echo "LDAPResults: " & objItem.LDAPResults
    Wscript.Echo "LDAPResultsPersec: " & objItem.LDAPResultsPersec
    Wscript.Echo "LDAPSearchcalls: " & objItem.LDAPSearchcalls
    Wscript.Echo "LDAPSearchcallsPersec: " & objItem.LDAPSearchcallsPersec
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "RecipientModifications: " & objItem.RecipientModifications
    Wscript.Echo "RecipientsPersec: " & objItem.RecipientsPersec
    Wscript.Echo "RecipientsQueueLength: " & objItem.RecipientsQueueLength
    Wscript.Echo "Timestamp_Object: " & objItem.Timestamp_Object
    Wscript.Echo "Timestamp_PerfTime: " & objItem.Timestamp_PerfTime
    Wscript.Echo "Timestamp_Sys100NS: " & objItem.Timestamp_Sys100NS
    Next

    ReplyDelete