Tuesday, July 29, 2014

Everyday Powershell - Part 26 - Check Server last boot time

Here's a quickie that was useful this week.
$servers = Get-ADComputer -filter {OperatingSystem -like "*Server*"}
$report = @()
foreach ($server in $servers){
    $temp = "" | Select Name, Lastboot
    $temp.name = $server.dnshostname
    if(test-connection $temp.name -Count 1 -quiet){
        $temp.lastboot = [datetime]::ParseExact(((Get-wmiobject -ComputerName $temp.name Win32_operatingSystem).lastbootuptime).Substring(0,12), "yyyyMMddHHmm", $null)
    }
    else{
        $temp.lastboot = $null
    }
    $report += $temp
}
$report | sort name

Original inspiration was from here;

You see the great thing with powershell is once you know how to do something on one server you can do it on many servers relatively easily. To support some of our older fleet we had to change that query from CIM to WMI and meant some trickery with the Datetime but that’s all pretty simple stuff.

This could be combined with the server shutdown comments script from part 11 to get the reason for the last reboot too!

No comments:

Post a Comment