WSH Scripting
Friday, March 21st, 2008Using WSH with WMI enables you to query lots of information of a system.
Here are some examples of code that I regularly use.
- Determine UpTime of a computer
123456789101112131415161718
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
- Ping a computer
12345678910111213
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
- Map a Networkdrive
12
Set
oNet = CreateObject(
"wscript.network"
)
oNet.MapNetworkDrive
"X:"
,
"\\"
& strComputer &
"\c$"
- Get Computers from an OU in Active Directory
123
Set
ObjOU = GetObject(
"LDAP://OU=Desktops,OU=Computers,DC=corp,DC=contoso,DC=com"
)
ObjOU.Filter = Array(
"Computer"
)
For
Each
ObjComp in ObjOU
- List running processes
12345678
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
- Count DiskErrors
1234
Set
colLoggedEvents = objWMIService.ExecQuery _
(
"Select * from Win32_NTLogEvent Where Logfile = 'System'"
_
&
" and SourceName = 'disk'"
)
iDiskErrors = colLoggedEvents.count
- Show logged on user
12345678
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
- Enumerate all profiles stored in Documents and Settings
12345678910
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
1234567891011121314Function
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
- Read and write an XML-file
1234567891011121314151617181920212223242526272829303132333435363738394041
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"
)