- <#
- .SYNOPSIS
- Demonstrates uptime using WMI
- .DESCRIPTION
- This script used Win32_ComputerSystem to determine how long your system
- has been running. This is a rewrite/improvement of sample 3 at
- http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx.
- .NOTES
- File Name : Get-UpTime.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Script Posted to:
- http://www.pshscripts.blogspot.com
- Original sample posted at:
- http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx
- .EXAMPLE
- PS c:\foo> .\Get-UpTime.Ps1
- System Up for: 1 days, 8 hours, 40.781 minutes
- #>
- ##
- # Start of script
- ##
- # Helper Function - convert WMI date to TimeDate object
- function WMIDateStringToDate($Bootup) {
- [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup)
- }
- # Main script
- $Computer = "." # adjust as needed
- $computers = Get-WMIObject -class Win32_OperatingSystem -computer $computer
- foreach ($system in $computers) {
- $Bootup = $system.LastBootUpTime
- $LastBootUpTime = WMIDateStringToDate($Bootup)
- $now = Get-Date
- $Uptime = $now - $lastBootUpTime
- $d = $Uptime.Days
- $h = $Uptime.Hours
- $m = $uptime.Minutes
- $ms= $uptime.Milliseconds
- "System Up for: {0} days, {1} hours, {2}.{3} minutes" -f $d,$h,$m,$ms
- }
- # End script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Tuesday 13 January 2009
Get-UpTime.ps1
Subscribe to:
Post Comments (Atom)
4 comments:
Bad numeric constant: 28..
At C:\users\rob.meier\Documents\Get-UpTime.ps1:28 char:4
+ 28. <<<< function WMIDateStringToDate($Bootup) {
+ CategoryInfo : ParserError: (28.:String) [], ParseException
+ FullyQualifiedErrorId : BadNumericConstant
I just checked the script and it works perfectly here.
You may have cut/pasted incorrectly. It looks like when you did the cut/paste, you may have accidentally included the line numbers.
Thank you. I found the mistake. The numbers for each line needed to be deleted.
Thomas-
I have about zero scripting experience, so you may not want to respond to my question, but how can I adapt this script to run against a list of computers in a single domain? I can create a text file with the computer names no problem. I am familiar with the concept of variables but not in the context of scripting.
Post a Comment