Thursday, March 5, 2009

I've been up for days!!

When was that machine last rebooted?

function Get-Uptime {
param([string]$computer = $env:computername)
$computer = $computer.toupper()
if (Test-Connection $computer -Count 1 -ea silentlycontinue) {
    $WMITimeString = (gwmi -Query "Select lastbootuptime from win32_operatingsystem" -computer $computer).lastbootuptime
    $bootTime = ([wmi]'').ConvertToDateTime($WMITimeString)
    $formattedTime = (Get-Date $bootTime).datetime
    Write-Host -fore green -Background black "Computer $computer has been up since:`n`n`t $formattedTime"
    }else {Write-Host -fore red -Background black "Computer $computer is not online."}

The script will check the local machine if no parameter is specified.

No comments: