Posts Tagged ‘WSH’

WSH Scripting

Friday, March 21st, 2008

Using WSH with WMI enables you to query lots of information of a system.
Here are some examples of code that I regularly use.

  1. Determine UpTime of a computer
    strComputer = InputBox("Enter computername","Determine Uptime",".")
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * From Win32_PerfFormattedData_PerfOS_System")
    intSystemUptime = 0
    For Each objOS in colOperatingSystems
    Dim intSystemUptimeSec
    Dim intSystemUptimeMin
    Dim intSystemUptimeHour
    Dim intSystemUptimeDay
    intSystemUptimeDay = Int(objOS.SystemUpTime / 86400)
    intSystemUptimeHour = Int(objOS.SystemUpTime / 3600) - (intSystemUptimeDay*24)
    intSystemUptimeMin = Int(objOS.SystemUpTime / 60) - (intSystemUptimeHour*60) - (intSystemUptimeDay*24*60)
    intSystemUptimeSec = Int(objOS.SystemUpTime) - (intSystemUptimeMin*60) - (intSystemUptimeHour*60*60) - (intSystemUptimeDay*24*60*60)
    intSystemUptime = Int(objOS.SystemUpTime / 60)
    MsgBox("Uptime for " & strcomputer &  " = " & intSystemUptimeDay & "d " & intSystemUptimeHour & "h " & intSystemUptimeMin & "m " & intSystemUptimeSec & "s")
    Next
  2. Ping a computer
    strComputer = InputBox("Enter computername","Ping a computer",".")
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
    ExecQuery("select * from Win32_PingStatus where address = '"_
    & strComputer & "'")
    For Each objStatus in objPing
    If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
    MsgBox("Ping failed")
    else
    MsgBox("Ping succeeded")
    End If
    Next
  3. Map a Networkdrive
    Set oNet = CreateObject("wscript.network")
    oNet.MapNetworkDrive "X:", "\\" & strComputer & "\c$"
  4. Get Computers from an OU in Active Directory
    Set ObjOU = GetObject("LDAP://OU=Desktops,OU=Computers,DC=corp,DC=contoso,DC=com")
    ObjOU.Filter = Array("Computer")
    For Each ObjComp in ObjOU
  5. List running processes
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process")
    For Each objProcess in colProcess
    WScript.Echo objProcess
    Next
  6. Count DiskErrors
    Set colLoggedEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where Logfile = 'System'" _
    & " and SourceName = 'disk'")
    iDiskErrors = colLoggedEvents.count
  7. Show logged on user
    Set Users = objWMIService.InstancesOf("Win32_ComputerSystem")
    for each User in Users
    If isNull(User.UserName) then
    WScript.Echo "No User is logged on"
    else
    WScript.Echo User.UserName
    end if
    Next
  8. Enumerate all profiles stored in Documents and Settings
    Set objFolder = objFSO.GetFolder("\\" & strComputer & "\c$\Documents And Settings")
    valStoredprofiles = ""
    For Each Subfolder in objFolder.SubFolders
    If IsStandardUserProfile(SubFolder.Path) then
    arrPath = split(Subfolder.Path,"\")
    'sSize = Round(SubFolder.Size/1024/1024,2)
    'valStoredprofiles = valStoredprofiles & arrPath(UBound(arrPath,1)) & "  [" & sSize & "MB] " & " - "
    valStoredprofiles = valStoredprofiles & arrPath(UBound(arrPath,1)) & " - "
    end if
    Next
    Function IsStandardUserProfile(sFolder)
    Dim iMatches
    iMatches = 0
    If Instr(sFolder,"Administrator") > 0 then iMatches = iMatches + 1
    If Instr(sFolder,"All Users") > 0 then iMatches = iMatches + 1
    If Instr(sFolder,"Default User") > 0 then iMatches = iMatches + 1
    If Instr(sFolder,"LocalService") > 0 then iMatches = iMatches + 1
    If Instr(sFolder,"NetworkService") > 0 then iMatches = iMatches + 1
    If iMatches = 0 then
    IsStandardUserProfile= true
    Else
    IsStandardUserProfile= false
    end if
    End Function
  9. Read and write an XML-file
    Set objXML = CreateObject("Microsoft.XMLDOM")
    objXML.load "result.xml"
    'WScript.Echo objXML.parseError.errorCode
    If (objXML.parseError.errorCode <> 0) Then
    Dim myErr
    Set myErr = objXML.parseError
    MsgBox("You have error " & myErr.reason)
    Else
    'WScript.Echo objXML.xml
    'WScript.Echo objXML.documentElement.attributes.item(0).nodeValue
    Dim i
    i = 1
    ReDim PreServe arrPcs(i+1)
    Set pcs = objXML.getElementsByTagName("pc")
    'WScript.Echo pcs
    for each pc in pcs
    arrPcs(i-1) = pc.getAttribute("id") & ";" & pc.getAttribute("location")
    i=i+1
    ReDim PreServe arrPcs(i)
    next
    End If
    for each pc in pcs
    pcID = pc.getAttribute("id")
    if pcID = current PC then
    '<ip>192.168.1.19</ip>
    '<uptime>0</uptime>
    '<diskerrors>0</diskerrors>
    '<user>NONE</user>
    '<storedprofiles>0</storedprofiles>
    'WScript.Echo pc.childNodes.length
    pc.childNodes.Item(0).firstChild.nodeValue = valIP
    pc.childNodes.Item(2).firstChild.nodeValue = valUptime
    pc.childNodes.Item(3).firstChild.nodeValue = valDiskerrors
    'WScript.Echo valUser
    pc.childNodes.Item(4).firstChild.nodeValue = valUser
    pc.childNodes.Item(5).firstChild.nodeValue = valStoredprofiles
    exit for
    end if
    next
    objXML.documentElement.Attributes.Item(0).nodeValue = now
    objXML.save("result.xml")