Mark's profileMark Sheppard's SpaceBlogLists Tools Help

Blog


    January 02

    PowerShell System Uptime

    I just needed to know how long it was since my box was last rebooted.  Windows Vista has a nice feature where this is now a property of the Performance tab in Task Manager.  But alas, there's no such nice functionality in Windows 2K3.  Usually I end up in PerfMon to get the number of seconds the box has been up and then in calculator to convert this into real time.

    Enter PowerShell to the rescue:

    function GLOBAL:Get-SystemBootupTime([String]$computer="localhost") { 
          $wmiOsInformation = Get-WmiObject -computer $computer -class Win32_OperatingSystem 
          $wmiOsInformation.ConvertToDateTime($wmiOsInformation.LastBootUpTime)

     
    function GLOBAL:Get-SystemUptime([String]$computer="localhost") { 
          $wmiPerfOsSystem = Get-WmiObject -computer $computer -class Win32_PerfFormattedData_PerfOS_System 
          $wmiPerfOsSystem.SystemUpTIme

     
    # Get System Bootup time
    PS C:\> Get-SystemBootupTime
    21 December 2006 17:16:02 
     
    # Get system uptime in seconds
    PS C:\> Get-SystemUptime
    1016328

    Not bad, I can now see the system uptime in seconds, but I want the result formatted

    function GLOBAL:Get-SystemUptimeFormatted([String]$computer="localhost") { 
          $wmiPerfOsSystem = Get-WmiObject -computer $computer -class Win32_PerfFormattedData_PerfOS_System 
          [TimeSpan] $systemUptime = New-TimeSpan -seconds $wmiPerfOsSystem.SystemUpTIme 
          [String]::Format("{0:G}", $systemUptime)

     
    PS C:\> Get-SystemUptimeFormatted
    11.18:53:16

    Job done! As a developer, I tend towards using .NET all over, so I've probably missed some rather subtle ways of achieving the formatting with PowerShell.

    You can get a lot of other useful information from the WMI classes that could be the focus of other functions:

    PS C:\> $computer = "localhost"
    PS C:\> $wmiOsInformation = Get-WmiObject -computer $computer -class Win32_OperatingSystem
    PS C:\> $wmiOsInformation 

    SystemDirectory : C:\WINDOWS\system32
    Organization    : My Org 
    BuildNumber     : 3790
    RegisteredUser  : Me
    SerialNumber    : xxxxx-xxx-xxxxxxx-xxxxx
    Version         : 5.2.3790

    To ensure that this handy set of functions is always there when needed, I've added them to my profile:

    PS C:\> Notepad $profile

     

    Comments (3)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Picture of Anonymous
    /\/\o\/\/ wrote:
    Sorry, if I'm spamming, but you can also run the command in the string like this :
     
    "$(New-TimeSpan -seconds $wmiPerfOsSystem.SystemUpTIme)"
     
    Jan. 2
    Picture of Anonymous
    /\/\o\/\/ wrote:
    in this case as G is the default we can do this even shorter :
     
    PoSH>"$systemuptime"
    11.18:18:48
     
    Greetings /\/\o\/\/
    Jan. 2
    Picture of Anonymous
    /\/\o\/\/ wrote:
    > I've probably missed some rather subtle ways of achieving the formatting with PowerShell.
     
    in PowerShell you can use the format operator ( -F ) as a shortcut :
     
    PoSH>"{0:G}" -f $systemUptime
    11.18:18:48
     
    Greetings /\/\o\/\/
    Jan. 2

    Trackbacks (1)

    The trackback URL for this entry is:
    http://mark-sheppard.spaces.live.com/blog/cns!1905A40C83BF050F!150.trak
    Weblogs that reference this entry