The script for today is created for Paul.
It is an extension of the previous server uptime script.
The uptime is now formatted in: xx days, xx hours, xx minutes.
Follow the next steps to run the script (admin rights needed for the WMI connections):
- copy and paste the script in your favorite text editor
- save the script (for example c:tempserveruptimev2.vbs)
- open a command prompt
- go to “c:temp”
- give “cscript serveruptimev2.vbs” (without quotes) and enter
The script:
' Name : serveruptimev2.vbs
' Description : script to enumerate the system uptime of all servers in Active Directory V2
' Author : dirk adamsky - deludi bv
' Version : 2.00
' Date : 15-07-2010
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=computer)(operatingSystem=*server*))"
strAttributes = "name"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
On Error Resume Next
strHostname = adoRecordset.Fields("name").Value
If CheckStatus(strHostname) = False Then
Wscript.Echo strHostname & " does not reply"
Else
Wscript.Echo strHostname & " is up for " & GetUptime(strHostname)
End If
adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set objRootDSE = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing
Function CheckStatus(strAddress)
Dim objPing, objRetStatus
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & strAddress & "'")
For Each objRetStatus In objPing
If IsNull(objRetStatus.StatusCode) Or objRetStatus.StatusCode <> 0 Then
CheckStatus = False
Else
CheckStatus = True
End If
Next
Set objPing = Nothing
End Function
Function GetUptime(strServer)
Set objDateTime = CreateObject("WbemScripting.SWbemDateTime")
Set objWMIService = GetObject("winmgmts:\" & strServer & "rootcimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
objDateTime.Value = objOS.LastBootUpTime
strMinutes = DateDiff("n", objDateTime.GetVarDate, Now)
If strMinutes =< 0 Then
strUptime = "0 days, 0 hours, 0 minutes"
Else
strUptime = ""
If strMinutes >= 1440 Then
strUptime = Round(strMinutes1440,0) & " days,"
End If
strMinutes = strMinutes Mod 1440
If strMinutes >= 60 Then
strUptime = strUptime & (strMinutes60) & " hours,"
End If
strMinutes = strMinutes Mod 60
GetUptime = strUptime & strMinutes & " minutes"
End If
Next
Set colOperatingSystems = Nothing
Set objWMIService = Nothing
Set objDateTime = Nothing
End Function
When you have problems/questions please post a reply or give a ‘star’ rating.
Happy scripting.
Best regards,
Dirk Adamsky – Deludi BV
[adrotate group="2" banner="3"]
