Uptime of Windows boxes
An cmdlet that is still missing in WMF5 as a default command.
So I decided to write a function for it.
This script determines the version of the WSMAN :
- 2.0 Windows 7,2008,2008R2
- 3.0 Windows 8 and higher
Because the WSMAN v2.0 is not CIM capable only WMI
Function Get-MyUpTime { <# .SYNOPSIS Get the uptime of windows boxes .DESCRIPTION Get The uptime of a single or multiple windows boxes This Function checks the WSMan version to determine the usage WMI or CIM The default check is done using CIM if the WSMANversion is not equal to 2.0 This script has been tested on - Windows 7 - Windows 7 SP1 - Windows 8 - Windows 8.1 - Windows 10 - Windows Server 2008 - Windows Server 2008 R2 - Windows Server 2012 - Windows Server 2012 R2 .EXAMPLE $Computer = (Get-ADcomputer -Filter {OperatingSystem -Like "Windows*"}).name Get-MyUpTime -Computer $Computer .EXAMPLE Get-MyUpTime -Computer 'Server1','Server3','Server2' .EXAMPLE $Computer = (Get-ADcomputer -Filter {OperatingSystem -Like "Windows*"}).name Get-MyUpTime -Computer $Computer | Sort-Object SystemName | Where-Object Day -GT 30 | Format-Table -AutoSize Using a ActiveDirectory Query and only showing the machines that have an uptime greater than 30 days .EXAMPLE Get-MyUpTime -Computer 'Server1','Server2','Server3' -Verbose | Format-Table -AutoSize #> #End SYNOPSIS [CmdletBinding()] Param ( [String[]]$Computer ) #End Param Function Output { Param ( [String]$LastReboot, [PSObject]$System) $Times = New-TimeSpan -Start $LastReboot -End ([datetime]::Now) $Data = [Ordered]@{ SystemName = $System.CSname LastBootUpTime = $LastReboot Day = $Times.days Hour = $Times.hours Minutes = $Times.minutes OS = ($System.name).Split('|')[0] } New-Object PSobject -Property $Data } #End Function Output $Computer = $Computer | Sort-object foreach ($Item in $Computer){ $Item = $Item.ToUpper() Write-Verbose $Item Try { Write-Verbose "Getting WSMAN Version for $Item" $WSMANVersion = (Test-WSMan -ComputerName $Item -ErrorAction Stop).InnerText.Split(':').Trim() | Select -Last 1 Switch ($WSMANVersion) { '2.0' { Write-Verbose "Running WMI PoSh 2 : WSMAN $WSMANVersion" $WMI = Get-WmiObject Win32_OperatingSystem -ComputerName $Item $Reboot = $WMI.ConvertToDateTime($WMI.LastBootUpTime) Output -lastreboot $Reboot -system $WMI} default { Write-Verbose "Running CIM PoSh 3+ : WSMAN $WSMANVersion" $Cim = Get-CimInstance Win32_OperatingSystem -CimSession $Item $Reboot = $Cim.LastBootUpTime Output -lastreboot $Reboot -system $Cim} } #End Switch } #End Try Catch { $Label = 'System not reachable using WSMAN' Write-Verbose "$Item : $Label" $Data = [Ordered]@{ SystemName = $Item OS = $Label } New-Object PSobject -Property $Data } #End Catch } #End foreach } #End Function Get-MyUpTime
Get-help Get-MyUpTime -Full for the full help of this function
Some examples for running the function
Get-MyUpTime -Computer 'Server01','Server02','Server03' | Format-Table -AutoSize
Output :
Systemname LastBootUpTime Day Hour Minutes OS ---------- -------------- --- ---- ------- -- SERVER01 19-12-2015 21:44:53 1 0 11 Microsoft Windows Server 2012 R2 Standard SERVER02 20-12-2015 12:58:32 0 8 58 Microsoft Windows 7 Professional N SERVER03 System not reachable using WSMAN
More Stories
Running PowerShell Hyper-V and VMWare Modules
Extend the Evaluation Period on Windows Server
Now using Ansible, Chocolatey, Powershell