I needed to check the server uptime for multiple servers, and well, I wanted to do it in PowerShell.
I found a sample from MSDN, and modified it, and threw it into a function.
Function Get-Uptime
{
#Requires -version 2
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
Param(
[parameter(Mandatory=$false,HelpMessage="Computer Name(s)",ValueFromPipeline=$true)]
[object[]]$ComputerName = "."
)
PROCESS
{
$WmiOS = Get-WMIObject -class Win32_OperatingSystem -computer $ComputerName
Function ConvertWMIDateToDateTime($BootTime)
{
[System.Management.ManagementDateTimeconverter]::ToDateTime($BootTime)
}
Function GetUptime($ComputerName)
{
$BootTime = $WmiOS.LastBootUpTime
$LastBootUpTime = ConvertWMIDateToDateTime($BootTime)
$Uptime = (Get-Date) - $lastBootUpTime
$days = $Uptime.Days
$hours = $Uptime.Hours
$min = $uptime.Minutes
$sec = $uptime.Seconds
[console]::ForegroundColor = "Green"
"$($ComputerName) has been up for: {0} days, {1} hours, {2} minutes and {3} seconds" -f $days,$hours,$min,$sec
[console]::ResetColor()
}
GetUptime $ComputerName
}
<#
.SYNOPSIS
Gets uptime of a system via WMI
.DESCRIPTION
Get-Uptime uses WMI (specificaly Win32_ComputerSystem) to get the uptime of a system.
The core component of the script was found at http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx
and addapted with additional functionality.
.EXAMPLE
PS c:\> Get-UpTime SERVER01
SERVER01 has been up for: 355 days, 23 hours, 59 minutes and 59 seconds
.EXAMPLE
PS c:\> Get-ExchangeServer | Get-Uptime
SERVER01 has been up for: 355 days, 23 hours, 59 minutes and 59 seconds
SERVER02 has been up for: 355 days, 23 hours, 59 minutes and 59 seconds
SERVER03 has been up for: 355 days, 23 hours, 59 minutes and 59 seconds
SERVER04 has been up for: 355 days, 23 hours, 59 minutes and 59 seconds
.LINK
Script Posted to:
http://everydaynerd.com
Adapted from sample posted at:
http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx
.NOTES
Function Name : Get-UpTime
Author: Dan Burgess
Email: nerd@everydaynerd.com
Script Requires: Powershell 2.x or higher
#>
}
Here’s an alternative to this function that outputs to a powershell object – rather than just writing to the screen. (Thanks for the encouragement Jeffery)
Function Get-Uptime
{
#Requires -version 2
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
Param(
[parameter(Mandatory=$true,HelpMessage="Computer Name(s)",ValueFromPipeline=$true)]
[object[]]$ComputerName
)
PROCESS
{
$System = Get-WMIObject -class Win32_OperatingSystem -computer $ComputerName
Function GetUptime($ComputerName)
{
$Bootup = $System.LastBootUpTime
$LastBootUpTime = $System.ConvertToDateTime($System.LastBootUpTime)
$now = Get-Date
$Uptime = $now - $lastBootUpTime
$NewObjectProperties = @{
ComputerName=$ComputerName
Days=$Uptime.Days;`
Hours=$Uptime.Hours;`
Minutes=$Uptime.Minutes;`
Seconds=$Uptime.Seconds;`
}
New-Object psobject -Property $NewObjectProperties
}
$Results = GetUptime $ComputerName
$Results | Select ComputerName, Days, Hours, Minutes, Seconds
}
<#
.SYNOPSIS
Gets uptime of a system via WMI
.DESCRIPTION
Get-Uptime uses WMI (specificaly Win32_ComputerSystem) to get the uptime of a system.
The core component of the script was found at http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx
and addapted with additional functionality.
.EXAMPLE
PS c:\> Get-UpTime server01
ComputerName : {server01}
Days : 27
Hours : 2
Minutes : 21
Seconds : 26
.EXAMPLE
PS c:\> Get-ExchangeServer | Get-Uptime | ft -AutoSize
ComputerName Days Hours Minutes Seconds
------------ ---- ----- ------- -------
{server01} 27 2 15 49
{server02} 130 6 40 23
.LINK
Script Posted to:
http://everydaynerd.com
Adapted from sample posted at:
http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx
.NOTES
Function Name : Get-UpTime
Author: Dan Burgess
Email: nerd@everydaynerd.com
Script Requires: Powershell 2.x or higher
#>
}
Happy PowerShelling!
Related posts:
Microsoft DPM
MicrosoftExchange
Nerd with a .45
PowerShell
It is even easier to convert the date time. All WMI objects in PowerShell have a convert to date time method.
$LastBootUpTime = $WMIOs.ConvertToDateTime($WMIOS.LastBootUpTime)
Instead of using [console] I’d suggest using a cmdlet, Write-Host. My last comment would be to write an object to the pipeline. What you have now can’t be written to a text file, sorted or anything else. But if all you want is the message that’s fine. Just know that this version can’t do anything but write to the screen.
Thanks Jeffery, I didn’t look at the get-member of that object, so thanks for the tip! I’ve posted an alternate of the function that outputs to an object, rather than just writing to a screen. The first go was just a quick and dirty, but I’ve not found a function that couldn’t use improvement yet!
Also, I usually use the write-host for the colors, instead of [console] but sometimes I like to change things up.