When Were These Computers Last Rebooted?
Here's a short one. We want to know when this box was last rebooted. Sure, it's in the SCOM DB, but this is probably faster. The Win32_OperatingSystem WMI object contains the LastBootupTime attribute. Beyond that, it's a simple matter of converting that from WMI-time to [datetime]$time.
And, because computers may be in the middle of rebooting right now, they might not be pingable., hence the simple ping test.
function Get-LastRebootTime {
param (
[Parameter(
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)][String[]]$ComputerName = @($env:COMPUTERNAME),
[int]$Timeout = 1000
);
begin {
$wmi = New-Object WMI;
$ping = New-Object system.net.NetworkInformation.Ping;
}
process {
foreach ($computer in $ComputerName) {
$object = $true | select -Property Name, Rebooted;
$object.Name = $computer.ToLower();
if ($ping.Send($computer, $Timeout).Status -eq 'Success') {
$object.Rebooted = $wmi.converttodatetime((Get-wmiobject -computer $computer win32_OperatingSystem -ErrorAction SilentlyContinue).lastbootuptime);
} else {
$object.Rebooted = "NoPing"
}
$object;
}
}
}