Tuesday, September 09, 2008

Some Usefull VBScript Codes

Using shell scripts like vbscript can make your work much easier specially when you are a sys admin, you can run them in login process or any other OS specified time or even run them in a custom time/way, there is several ways to run them but I prefer to save the text in a text file with the extension of .vb like loginScript.vb it would be run like a executable remember that you should have installed Windows Host Scripting on machine, here is some small vbscript snippets :

Basic output


  • Wscript.Echo "Hello World,this is one way to display output..."

  • MsgBox "Hello World," & "this is another way..."


  • Basic input


  • Dim V
    V = InputBox("What the heck?")

  • Getting if user belonged to specified group


  • 'returns boolean

    Set oNetwork = CreateObject("WScript.Network")
    sDomain = oNetwork.UserDomain
    sUser = oNetwork.UserName
    bIsMember = False
    Set oUser = GetObject("WinNT://" & sDomain & "/" & _
    sUser & ",user")

    For Each oGroup In userObj.Groups
    If oGroup.Name = "Domain Users" Then
    bIsMember = True
    Exit For
    End If
    Next

  • Mapping


  • 'mapping drive
    Set oNet = WScript.CreateObject("WScript.Network")
    oNet.MapNetworkDrive "X:", "\\theServer\XFiles)

  • 'Mapping Printer
    Set oNet = WScript.CreateObject("WScript.Network")
    oNet.AddWindowsPrinterConnection "\\PrintServer\SecretPrinter"
    oNet.SetDefaultPrinter "\\PrintServer\SecretPrinter"

  • Getting IP address


  • 'localIp is the result
    Set myObj = GetObject("winmgmts:{impersonationLevel=impersonate}" & _
    "!//localhost".ExecQuery_
    ("select IPAddress from " & _
    "Win32_NetworkingAdapterConfiguration" & _" where IPEnabled=TRUE")
    'Go through the addresses
    For Each IPAddress in myObj
    If IPAddress.IPAddress(0) <> "0.0.0.0" Then
    LocalIP = IPAddress.IPAddress(0)
    Exit For
    End If
    Next
  •