HCHTech
Well-Known Member
- Reaction score
- 4,197
- Location
- Pittsburgh, PA - USA
This is a simple script someone posted for running in Solarwinds RMM to count the number of specific event log entries and fail if the count exceeds a threshold. I'm trying to troubleshoot this on a system here and it's not really working as desired. I'm sure it's because I don't understand dates well enough in powershell.
So, just reading through the script,
Line one sets the variable "startdate" to the value of today minus 14 days.
Line two seems to have a mistake, it sets the variable "ID" (presumably to "True") for every line in the system event log where the event ID is equal to 6008 and the timestamp on the event is less than the date set in line one. It seems to me that this line SHOULD use the -gt comparison operator so the the test is "(eventid = 6008) and (timestamp is greater than the date set in line one). Note - This comparison might also be done using either the -before or -after operators, but that escapes me as well currently.
Line three compares the total count of the True ID variables to 3. If the total is less than 3, then output "System Normal" and exit with a zero code. If the total is greater than or equal to 3, then output the error text and exit with a 1001 code.
SO, to test the logic of the script, I exported the system log on a system I have here and opened it in Excel so I could count the entries. I didn't have any eventid 6008s, but I had a bunch of eventID 7000s, so I'm testing that. In the last 7 days, I had 5 event 7000s in my system log. In the entirety of the system log, there are 144 event 7000 entries.
So I open a powershell window, and type in the first command as:
No errors, so to test, I type just the name of the variable, $startdate. I get the following
Tuesday, December 19, 2017 5:07:01 PM
Ok, this is exactly 7 days prior to the time I entered the command to set this variable. So far, so good.
Next, I type the second command as originally posted:
It thinks a while, but no errors, so to test, I type $ID.count
I get 144. In other words, this is the total count of all eventID 7000 events in the log. It looks like it thinks all events have a timestamp of less than $startdate.
So now I try it again but replace the 2nd -lt with a -gt, which makes more sense to me. I want a count of events that happened SINCE $Startdate, not PRIOR TO $Startdate.
So I type
Again, it thinks a while, but no errors, so to test, I type $ID.Count
I get 0, even though I know there are 5 event log entries newer than $startdate with eventID 7000. So, I don't think the value $_.TimeCreated is being picked up correctly by the EventLog call. Or, the variable $Startdate, as created here, isn't in a form that can be used in the comparison. I couldn't land on a way to output the TimeCreated value for a specific event, so I couldn't test this theory.
Maybe the timestamps for event log entries are stored in a non-date format? Remember Lotus 123? It stored dates as sequential numbers starting with January 1st 1900. So January 1, 1979 was stored as 28,854. Just a thought.
Also, giving the original poster of the script some credit, it must have worked on their system as written....
I've read through the get-help entries on get-eventlog, but of course, none of the examples are quite on point. Can someone point me in the right direction?
$startdate = ((Get-Date).Adddays(-14))
$ID = get-EventLog system | where {$_.eventid -match "6008" -and $_.TimeCreated -lt $startdate} | measure-Object
If ($ID.Count -lt 3)
{Write-Output "System Normal"
Exit 0 }
Else
{Write-Output "> 3 Unexpected Shutdowns in 2 weeks"
Exit 1001}
So, just reading through the script,
Line one sets the variable "startdate" to the value of today minus 14 days.
Line two seems to have a mistake, it sets the variable "ID" (presumably to "True") for every line in the system event log where the event ID is equal to 6008 and the timestamp on the event is less than the date set in line one. It seems to me that this line SHOULD use the -gt comparison operator so the the test is "(eventid = 6008) and (timestamp is greater than the date set in line one). Note - This comparison might also be done using either the -before or -after operators, but that escapes me as well currently.
Line three compares the total count of the True ID variables to 3. If the total is less than 3, then output "System Normal" and exit with a zero code. If the total is greater than or equal to 3, then output the error text and exit with a 1001 code.
SO, to test the logic of the script, I exported the system log on a system I have here and opened it in Excel so I could count the entries. I didn't have any eventid 6008s, but I had a bunch of eventID 7000s, so I'm testing that. In the last 7 days, I had 5 event 7000s in my system log. In the entirety of the system log, there are 144 event 7000 entries.
So I open a powershell window, and type in the first command as:
Code:
$startdate = ((Get-Date).Adddays(-7))
No errors, so to test, I type just the name of the variable, $startdate. I get the following
Tuesday, December 19, 2017 5:07:01 PM
Ok, this is exactly 7 days prior to the time I entered the command to set this variable. So far, so good.
Next, I type the second command as originally posted:
$ID = get-EventLog system | where {$_.eventid -match "7000" -and $_.TimeCreated -lt $startdate} | measure-Object
It thinks a while, but no errors, so to test, I type $ID.count
I get 144. In other words, this is the total count of all eventID 7000 events in the log. It looks like it thinks all events have a timestamp of less than $startdate.
So now I try it again but replace the 2nd -lt with a -gt, which makes more sense to me. I want a count of events that happened SINCE $Startdate, not PRIOR TO $Startdate.
So I type
$ID = get-EventLog system | where {$_.eventid -match "7000" -and $_.TimeCreated -gt $startdate} | measure-Object
Again, it thinks a while, but no errors, so to test, I type $ID.Count
I get 0, even though I know there are 5 event log entries newer than $startdate with eventID 7000. So, I don't think the value $_.TimeCreated is being picked up correctly by the EventLog call. Or, the variable $Startdate, as created here, isn't in a form that can be used in the comparison. I couldn't land on a way to output the TimeCreated value for a specific event, so I couldn't test this theory.
Maybe the timestamps for event log entries are stored in a non-date format? Remember Lotus 123? It stored dates as sequential numbers starting with January 1st 1900. So January 1, 1979 was stored as 28,854. Just a thought.
Also, giving the original poster of the script some credit, it must have worked on their system as written....
I've read through the get-help entries on get-eventlog, but of course, none of the examples are quite on point. Can someone point me in the right direction?
Last edited by a moderator: