none
Systeminfo Boot Time action in Powershell RRS feed

  • Question

  • Hello, I am trying to configure a poor mans alert when a set of systems is rebooted.  Rather than having some system specific startup script on each machine, I was thinking I could use a utility machine to run a systeminfo /s remotecomputer | find "System Boot Time" on each machine.  Anyone know of a way to use Powershell with if/else logic that would actually look at the time/date stamp and act on anything less than say, 5 minutes ago?  Normally the output of this command looks like this:

    System Boot Time:          8/12/2015, 9:44:42 AM

    I am aware of how to script the alert in my preferred method, really just need help with Powershell code necessary to analyze the time/date from this command and return a success/failure based on whether or not it occurred with the last 5 minutes of current time.

    Or if there is a better way, I am open to suggestions.  Thanks!

    Wednesday, August 12, 2015 5:43 PM

Answers

  • PowerShell example:


    $minutes = 5
    $computerName = "."
    $os = Get-WmiObject Win32_OperatingSystem -ComputerName $computerName
    $lastBootUpTime = $os.ConvertToDateTime($os.LastBootUpTime)
    if ( ((Get-Date) - $lastBootUpTime).TotalMinutes -le $minutes ) {
      "Computer was rebooted in the last $minutes minutes"
    }
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 12, 2015 9:15 PM
    Moderator

All replies

  • Hi,

    I'd query Win32_OperatingSystem and look at the LastBootUpTime property instead of using systeminfo.


    • Proposed as answer by jrv Wednesday, August 12, 2015 6:27 PM
    Wednesday, August 12, 2015 5:53 PM
  • Sweet, can you give me any more guidance?  after the WMI query, what would the line look like that checked if this time was within x amount of minutes?
    Wednesday, August 12, 2015 8:15 PM
  • PowerShell example:


    $minutes = 5
    $computerName = "."
    $os = Get-WmiObject Win32_OperatingSystem -ComputerName $computerName
    $lastBootUpTime = $os.ConvertToDateTime($os.LastBootUpTime)
    if ( ((Get-Date) - $lastBootUpTime).TotalMinutes -le $minutes ) {
      "Computer was rebooted in the last $minutes minutes"
    }
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 12, 2015 9:15 PM
    Moderator