Use the following PowerShell commands to find out how long the server has been up for since the last restart.
PS C:\> $wmi = Get-WmiObject -Class Win32_OperatingSystem
PS C:\> $wmi.ConvertToDateTime($wmi.LocalDateTime) –
$wmi.ConvertToDateTime($wmi.LastBootUpTime)
To display the uptime in formatted output, use the following commands:
PS C:\Windows\system32> $name = "bcdpprd01"
PS C:\Windows\system32> $wmi = Get-WmiObject -Class win32_operatingsystem -ComputerName $name
PS C:\Windows\system32> $uptime = $wmi.ConvertToDateTime($wmi.LocalDateTime) - $wmi.ConvertToDateTime($wmi.LastBootUpTime)
PS C:\Windows\system32> Write-Output "Uptime for $($name): $($uptime.Days) Day(s), $($uptime.hours) Hour(s), $($uptime.minutes) Minute(s), $($uptime.seconds) Second(s)"
Output will be displayed as:
Uptime for bcdpprd01: 0 Day(s), 15 Hour(s), 2 Minute(s), 4 Second(s)
To check the uptime for multiple Windows Servers, create a text file with a list of servers and create a Powershell script with a loop construct like foreach. The beauty of the following script is that it does not require uptime.exe to check uptime for any Windows Server(s).
$servers = Get-Content "C:\Nirav Store\servers.txt"
@(
foreach ($server in $servers)
{
if ( Test-Connection -ComputerName $server -Count 1 -ErrorAction SilentlyContinue )
{
$wmi = Get-WmiObject -Class win32_operatingsystem -ComputerName $server
$uptime = $wmi.ConvertToDateTime($wmi.LocalDateTime) - $wmi.ConvertToDateTime($wmi.LastBootUpTime)
Write-Output "Uptime for $($server): $($uptime.Days) Day(s), $($uptime.hours) Hour(s), $($uptime.minutes) Minute(s), $($uptime.seconds) Second(s)"
}
Else
{
Write-output "$server is not pingable."
}
}
) | Out-file -FilePath "c:\Nirav Store\results.txt"
If you do not want to redirect the output to a text file, do not pipe the output to a text file, which means, omit “| Out-file -FilePath "c:\Nirav Store\results.txt"”.
PS C:\> $wmi = Get-WmiObject -Class Win32_OperatingSystem
PS C:\> $wmi.ConvertToDateTime($wmi.LocalDateTime) –
$wmi.ConvertToDateTime($wmi.LastBootUpTime)
To display the uptime in formatted output, use the following commands:
PS C:\Windows\system32> $name = "bcdpprd01"
PS C:\Windows\system32> $wmi = Get-WmiObject -Class win32_operatingsystem -ComputerName $name
PS C:\Windows\system32> $uptime = $wmi.ConvertToDateTime($wmi.LocalDateTime) - $wmi.ConvertToDateTime($wmi.LastBootUpTime)
PS C:\Windows\system32> Write-Output "Uptime for $($name): $($uptime.Days) Day(s), $($uptime.hours) Hour(s), $($uptime.minutes) Minute(s), $($uptime.seconds) Second(s)"
Output will be displayed as:
Uptime for bcdpprd01: 0 Day(s), 15 Hour(s), 2 Minute(s), 4 Second(s)
To check the uptime for multiple Windows Servers, create a text file with a list of servers and create a Powershell script with a loop construct like foreach. The beauty of the following script is that it does not require uptime.exe to check uptime for any Windows Server(s).
$servers = Get-Content "C:\Nirav Store\servers.txt"
@(
foreach ($server in $servers)
{
if ( Test-Connection -ComputerName $server -Count 1 -ErrorAction SilentlyContinue )
{
$wmi = Get-WmiObject -Class win32_operatingsystem -ComputerName $server
$uptime = $wmi.ConvertToDateTime($wmi.LocalDateTime) - $wmi.ConvertToDateTime($wmi.LastBootUpTime)
Write-Output "Uptime for $($server): $($uptime.Days) Day(s), $($uptime.hours) Hour(s), $($uptime.minutes) Minute(s), $($uptime.seconds) Second(s)"
}
Else
{
Write-output "$server is not pingable."
}
}
) | Out-file -FilePath "c:\Nirav Store\results.txt"
If you do not want to redirect the output to a text file, do not pipe the output to a text file, which means, omit “| Out-file -FilePath "c:\Nirav Store\results.txt"”.
Comments
Post a Comment