This article explains how one can check the uptime of window computer or multiple computers and one can also use the same script to uptime report.
The below script helps you find the uptime or last boot time of a computer or multiple remote computers, generally, system administrators check the uptime of servers and desktops on daily basis to see the reboot cycle is working or not. There are various tools available to check the uptime of machines, however, using PowerShell is very easy and simple.
This article explains how to get the uptime of any computer, I have written a PowerShell script to find uptime of bulk remote computers. For multiple remote computer checks, you need to have the hostnames or IP addresses details saved in the notepad file.
function get-uptime { param( $computername =$env:computername ) $osname = Get-WmiObject win32_operatingsystem -ComputerName $computername -ea silentlycontinue if($osname) { $lastbootuptime =$osname.ConvertTodateTime($osname.LastBootUpTime) $LocalDateTime =$osname.ConvertTodateTime($osname.LocalDateTime) $up =$LocalDateTime - $lastbootuptime $uptime ="$($up.Days) days, $($up.Hours)h, $($up.Minutes)mins" $output =new-object psobject $output |Add-Member noteproperty LastBootUptime $LastBootuptime $output |Add-Member noteproperty ComputerName $computername $output |Add-Member noteproperty uptime $uptime $output | Select-Object computername,LastBootuptime,Uptime } else { $output =New-Object psobject $output =new-object psobject $output |Add-Member noteproperty LastBootUptime "Not Available" $output |Add-Member noteproperty ComputerName $computername $output |Add-Member noteproperty uptime "Not Available" $output | Select-Object computername,LastBootUptime,Uptime } } get-uptime
if you execute the above code, just the call the function “get-uptime” you will get current computer uptime, here is the output.
Get Uptime for Multiple Remote Computers
We use the same script for pulling the multiple remote computer uptime, we just need to add a few additional lines of code, see how it looks.
We need to create a file for storing all the servers or multiple machines lists, I named itas Allserver.txt for and saved it in my local machine c:\temp\Allserver.txt
$multiplemachines =@() $allservers =Get-Content -Path "C:\temp\Allserver.txt" foreach($allserver in $allservers){ $multiplemachines += get-uptime $allserver } $multiplemachines |ft -AutoSize
Let’s put together the code and how the complete script looks as below.
function get-uptime { param( $computername =$env:computername ) $osname = Get-WmiObject win32_operatingsystem -ComputerName $computername -ea silentlycontinue if($osname) { $lastbootuptime =$osname.ConvertTodateTime($osname.LastBootUpTime) $LocalDateTime =$osname.ConvertTodateTime($osname.LocalDateTime) $up =$LocalDateTime - $lastbootuptime $uptime ="$($up.Days) days, $($up.Hours)h, $($up.Minutes)mins" $output =new-object psobject $output |Add-Member noteproperty LastBootUptime $LastBootuptime $output |Add-Member noteproperty ComputerName $computername $output |Add-Member noteproperty uptime $uptime $output | Select-Object computername,LastBootuptime,Uptime } else { $output =New-Object psobject $output =new-object psobject $output |Add-Member noteproperty LastBootUptime "Not Available" $output |Add-Member noteproperty ComputerName $computername $output |Add-Member noteproperty uptime "Not Available" $output | Select-Object computername,LastBootUptime,Uptime } } get-uptime $multiplemachines =@() $allserver =Get-Content -Path "C:\temp\Allserver.txt" foreach($oneserver in $allserver) { $multiplemachines += get-uptime $oneserver } $multiplemachines |ft -AutoSize
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
.
Leave a Reply Cancel reply