Uptime script - comments, critiques?

Showing 1-1 of 1 messages
Uptime script - comments, critiques? Alex K. Angelopoulos at 9/5/08 10:03 AM
I'm working on a generic uptime script that might benefit from having a few
people thump on the ugly bits. Anyone care to try this? The intent is to
allow it to return uptime checks on multiple systems, complete with
essential information such as the boot time and the time of the check. I'm
pretty certain I need to change a couple of items, including adding some
kind of display hinting to the output instead of using the ugly AsPSObject
parameter. BTW, PS 1 compatibility isn't a strict requirement for this; I'm
not up to speed on PS 2 scripting details yet, so there may be some design
changes that are a good idea here.

Param([string[]]$ComputerName = @("."),

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}