/build/static/layout/Breadcrumb_cap_w.png

Windows Uptime in Seconds in a Custom Inventory Rule using Powershell

Here's an example of how you can use powershell to extract more complex values than you can get from just using wmic or registry reads...

Uptime is a simple example.  Yes, you can use filters such as Uptime > 4 days in your searches or smart labels, so for practical purposes maybe not wonderful, but it is a simple example.

Since WMI returns uptime as a reference to the last reboot of the system, we need to do something a little bit more complex to get the number of seconds of uptime, like you can get in a *nix based system.

Since all of our systems have Powershell available I decided to leaverage that for our solution.

[int]((get-date)-[system.management.managementdatetimeconverter]::todatetime((get-wmiobject -class win32_operatingsystem).Lastbootuptime)).TotalSeconds

Let's break this down

[int] means we are casting (in other words converting) the result to an integer.  By default date/time math will result in floating point results much of the time, and we want a simple integer value instead.

(get-date) returns an object with the current system date and time, on the computer from which it is being run.

[system.management.managementdatetimeconverter]::todatetime references a system assembly for a function to convert system date formats into the general date/time type used by .net (or in this case Powershell.)

(get-wmiobject -class win32_operatingsystem).Lastbootuptime is one way in Powershell that you can retrieve the date/time of the last system startup.

So if you follow the parenthesis, we're subtracting the last boot date/time from the current date/time, which results in a date/time span object, which has properties, one of which is TotalSeconds.  We could just as easily return the span as minutes, hours, days, etc.

Running this from powershell returns the uptime in seconds, so to implement this as a Custom Inventory Rule, just use the following.

Or to make it a bit easier to implement you can cut and past the following:

ShellCommandNumberReturn(powershell [int]((get-date)-[system.management.managementdatetimeconverter]::todatetime((get-wmiobject -class win32_operatingsystem).Lastbootuptime)).TotalSeconds)

This is just an example of what you can do using the ShellCommand* methods with Powershell.  Generally you can decant most expressions into a single line of code returning just what you want which means you can avoid posting script files on shares or using the scripting interfaces, when all you want is to add custom inventory values.


Comments

  • Without powershell, you could use something like the following:

    ShellCommandNumberReturn(wmic os get lastbootuptime)

    Running from a command prompt:

    C:\Users\Jason>wmic os get lastbootuptime
    LastBootUpTime
    20121101020148.540310-300 - jknox 11 years ago
    • That gives you when the LastBootUpTime was, not how long it has been since at the time inventory ran. The above poweshell is giving you how long in seconds (since how long is already in the inventory). - ncsutmf 11 years ago
  • Alternately, instead of subtracting the last bootup time from the current date-time returned from "Get-Date" you can use the New-TimeSpan cmdlet with the lastbootuptime as the start time.

    PS C:\> [int] (New-TimeSpan -Start ([system.management.managementdatetimeconverter]::todatetime((Get-WmiObject -Class Win32_OperatingSystem).LastBootupTime))).TotalSeconds - ncsutmf 11 years ago
    • Most excellent, I like that even better! - xoanan 11 years ago
This post is locked

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ