Jump to content

Need Help with Custom system uptime script


Recommended Posts

Trying to get a custom script to obtain system uptime in hours and if uptime is greater than 24 hours send a console message to logged in user.  Here is what I have so far, but the $Final variable will not populate properly.  Also would like to pass the final uptime to an output variable which is tied to a custom field (system level) for use in workflows. 

Quote

# Outputs
$HoursUp = 0
$Final = 0
function Get-Uptime {
   $os = Get-WmiObject win32_operatingsystem
   $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
   $Display = "Display Uptime: " + $uptime.TotalHours + " hours"
   $Final = $uptime.TotalHours
   Write-Output $Display
}

function NotifyLocalConsole {
msg console /server:localhost ("This System has not been restarted for " + $Final + "hours.  Please Restart this machine when possible.")
}

Get-Uptime

NotifyLocalConsole
if ($Final > 24) {NotifyLocalConsole}
$HoursUp = $Uptime.TotalHours

Any help would be greatly appreciated

Link to post
Share on other sites
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...