Jump to content

Script to send alert about machines that have uptime for X amount of days (14 days)


JordanT

Recommended Posts

At the moment I'm trying to get a script to register on event id 4624 to check if the computer has been online for 14 days or more and this isn't working sadly if i could get any tips or improvements


 

# Outputs
$SETCUSTOMFIELDNO = "SHORT"
$SETCUSTOMFIELDYES = "LONG"
$days = 14
$system = Get-WmiObject win32_operatingsystem

if($system.ConvertToDateTime($system.LastBootUpTime) -lt (Get-Date).AddDays(-$days)){
    Start-Process -FilePath "$env:PWY_HOME\CLI.exe" -ArgumentList ("setVariable SETCUSTOMFIELDNO ""$SETCUSTOMFIELDNO""") -Wait

}else{
    Start-Process -FilePath "$env:PWY_HOME\CLI.exe" -ArgumentList ("setVariable SETCUSTOMFIELDYES ""$SETCUSTOMFIELDYES""") -Wait


}

 

Link to comment
Share on other sites

I am not a Pusleway user anymore, but there are a couple of ways you could do this.  You mention event ID, but I don't see any logic in your script to write an event ID to the event log.

However, that would probably be the easiest way, and then have Pulseway look for (and alert off of) that event entry.  I always set up a custom Event Log and then use my own codes so that I never have to worry about looking through logs with other stuff in it.  All my stuff goes to a custom log I set up.  

As far as your code, your logic seems, a bit off to me unless I'm just too tired lol, but your first if statement, your essentially saying if lastboot is BEFORE 14 days ago, SetCustomeFieldNo... Would you want that to be a yes, since that means it hasn't rebooted in over 14 days? I think your statements are backwards.  I assume your SetCustomField Yes would be used for if it's been up for over 14 days. Maybe I'm just reading it wrong.

I personally wouldn't bother with setting the custom fields. I would again, just write to whatever event log you want, with whatever event ID you want, set Pulseway to alert off that event ID, and then schedule your script to run once a day.  As soon as it runs and sees a machine over 14 days, it creates event log entry which Pulseway will then alert you to. 

Link to comment
Share on other sites

7 minutes ago, Mark G38 said:

I am not a Pusleway user anymore, but there are a couple of ways you could do this.  You mention event ID, but I don't see any logic in your script to write an event ID to the event log.

However, that would probably be the easiest way, and then have Pulseway look for (and alert off of) that event entry.  I always set up a custom Event Log and then use my own codes so that I never have to worry about looking through logs with other stuff in it.  All my stuff goes to a custom log I set up.  

As far as your code, your logic seems, a bit off to me unless I'm just too tired lol, but your first if statement, your essentially saying if lastboot is BEFORE 14 days ago, SetCustomeFieldNo... Would you want that to be a yes, since that means it hasn't rebooted in over 14 days? I think your statements are backwards.  I assume your SetCustomField Yes would be used for if it's been up for over 14 days. Maybe I'm just reading it wrong.

I personally wouldn't bother with setting the custom fields. I would again, just write to whatever event log you want, with whatever event ID you want, set Pulseway to alert off that event ID, and then schedule your script to run once a day.  As soon as it runs and sees a machine over 14 days, it creates event log entry which Pulseway will then alert you to. 

Yeah I think I am trying to make it too complicated and I'm 99% sure you're correct about the customfields being the wrong way because, I am using a workflow to add a tag and all my computers just about put themselves into "Uptime" tag i made

Link to comment
Share on other sites

  • 4 weeks later...

Ive had issues with using custom field and workflows, im not sure the script itself fails unless there is code after the "Custom Field" Code.

I have a ticket in as well as this forum post.

As for the code itself, here is the the code we used for finding 20 day uptime. Fits it in a one liner at least.
Ours works a bit differently, but will notify user and restart if over 20 days. I simplified code for the forum.
 

if((Get-CimInstance Win32_OperatingSystem | Select-Object LastBootUpTime).LastBootUpTime.AddDays(20) -lt (Get-Date))
{
shutdown -r -t 5400 -f
}

 

Edited by haylebop
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...