#Get-Uptime.ps1
Param([string[]]$ComputerName = @("."),
[switch]$AsPSObject)
foreach($computer in $ComputerName){
$os = Get-WmiObject Win32_OperatingSystem `
-ComputerName $computer
$LastBootUpTime = $os.ConvertToDateTime($os.LastBootUpTime)
$LocalDateTime = $os.ConvertToDateTime($os.LocalDateTime)
#Calculate uptime - this is automatically a timespan
$up = $LocalDateTime - $LastBootUpTime
#Create a simple text string representation of the uptime;
# unfortunately only English
# Maybe drop this? It's UNIX-uptime like, but ugly.
$text = "$($os.CSName) up $($up.Days) days, $($up.Hours) hours," +
" $($up.Minutes) minutes, $($up.Seconds) seconds"
# Add bootuptime DateTime value
$up = Add-Member -MemberType NoteProperty `
-Name LastBootUpTime -Value $LastBootUpTime `
-InputObject $up -PassThru |
Add-Member -MemberType NoteProperty `
-Name LocalDateTimestamp -Value $LocalDateTime -PassThru |
Add-Member -MemberType NoteProperty `
-Name ComputerName -Value $os.csname -PassThru |
Add-Member -MemberType NoteProperty `
-Name Text -Value $text -PassThru
if($AsPSObject){$up | Select-Object}else{$up}
}