test.ps1
| $os = Get-WmiObject win32_operatingsystem | | $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime)) | | $Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes" | | Write-Output $DisplayCOPY |
执行效果:
C:\>powershell -f test.ps1
Uptime: 0 days, 4 hours, 16 minutes
|
Get-Uptime.ps1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | param | | ( | | [Parameter(Position=0,ValuefromPipeline=$true)][string][alias("cn")]$computer, | | [Parameter(Position=1,ValuefromPipeline=$false)][string]$computerlist | | ) | | | | If (-not ($computer -or $computerlist)) | | { | | $computers = $Env:COMPUTERNAME | | } | | | | If ($computer) | | { | | $computers = $computer | | } | | | | If ($computerlist) | | { | | $computers = Get-Content $computerlist | | } | | | | foreach ($computer in $computers) | | { | | $Computerobj = "" | select ComputerName, Uptime, LastReboot | | $wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem" | | $now = Get-Date | | $boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime) | | $uptime = $now - $boottime | | $d =$uptime.days | | $h =$uptime.hours | | $m =$uptime.Minutes | | $s = $uptime.Seconds | | $Computerobj.ComputerName = $computer | | $Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec" | | $Computerobj.LastReboot = $boottime | | $Computerobj | | }COPY |
执行效果:
C:\>powershell -f Get-Uptime.ps1
ComputerName Uptime LastReboot
------------ ------ ----------
HAT 0 Days 4 Hours 23 Min 43 Sec 2015/4/24 8:22:51
|
转自 http://blogs.technet.com/b/bshukla/archive/2010/12/09/powershell-script-to-report-uptime.aspx |