PowerShell Function to get uptime on multiple computers

I wanted to create a function that I could use to find the uptime of several workstations. I did not want to read a list of machine name from csv, I just wanted pass a list of workstation names and get their uptime back. I also added a ping check to make sure the machine is alive.

 

Function Get-Computer-LastBootTime {
$Args | ForEach-Object -Process {
$ping = gwmi Win32_PingStatus -Filter ("Address='" + $_ + "'") | Select-Object StatusCode
if ($ping.statusCode -eq 0) {

$wmi = gwmi Win32_OperatingSystem -EA silentlycontinue -ComputerName $_
$localdatetime = $wmi.ConvertToDateTime($wmi.LocalDateTime)
$lastbootuptime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $localdatetime - $lastbootuptime
$days=$uptime.Days
$hours=$uptime.Hours
$mins=$uptime.Minutes
echo "$_ uptime: $days days $hours hours $mins mins"

}
else {
echo "$_ is offline"
}
} 
}

Comments are closed.