days since restart

The following script runs against a list of servers and determines the last restart time. It will report if

  1. the machine is not found via ping
  2. there is another kind of error with WMI or
  3. the last restart date is greater than a hard-coded value

The list of servers is pulled from AD and the servers ou. There are other OUs nested inside this OU and these will return blank lines in the object searcher. We ignore them. Currently we have no information on vh-(movies) servers, we ignore then. If the server can not be pinged we report on this as “not found”. Older 2005 Servers seem to have rpc issues and we report these as “errors”.

Otherwise we get the lastbootuptime from the win32_operatingsystem object, compare it to the current date minus $daysSincerestart and report if the difference is greater than zero. This report is to the console, but could easily be output to a file or html document to email to an administrator.

$error.clear()
$servers = @()
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://ads.university.edu/ ou=servers, DC=university,DC=edu")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$objUser = $objResult.GetDirectoryEntry()
$server = ($objuser.sAMAccountName).tostring()
$servers += $server.replace("$","")
} $daysSinceRestart = 90 write-output "testing restart older than $daysSinceRestart days" foreach ($server in $servers){
if (($server -ne "") -and ($server -notlike "VH*")){ try {
# write-output "$server"
if (Test-Connection -CN $server -Count 1 -BufferSize 16 –Quiet){ $theserver = Get-WmiObject win32_operatingsystem -computername $server -ErrorAction Stop
$a = $theserver|select @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
if(($a.lastbootuptime) -gt $(get-date).AddDays(-$daysSinceRestart)){
# write-host "the last restart date of $($a.lastbootuptime) for $server is less than $daysSinceRestart days"
}
else
{
write-host "the last restart of $server is on $($a.LastBootUpTime) which is more than $daysSinceRestart days"
}
}
else {
write-output "$server can not be located via ping"
} # end of if ping
} #end of try
catch {
write-output "ERROR working with $server"
} # end of catch }
} # end of server not blank
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.