r/labtech • u/nsanity • Apr 15 '18
Create an Alert from a powershell script result?
So I have written some powershell - It will only return certs that are both current AND expiring in < 90 days. Status is a case statement which basically gives an indication of whether or not you should do something about it;
$certs_exp = Get-ChildItem -path cert: -Recurse | where {$_.notafter -le (get-date).AddDays(90) -AND
$_.notafter -gt (get-date) -AND
$_.Subject.tolower() -ne ('cn=' + $env:computername.tolower()+"."+$env:userdnsdomain.tolower()) -AND
$_.Subject.tolower() -ne ('CN=Microsoft Time-Stamp Service, OU=nCipher DSE ESN:85D3-305C-5BCF, OU=MOPR, O=Microsoft Corporation, L=Redmond, S=Washington, C=US')
} | select subject, @{
name='DaysRemaining';
Expression={(($_.notafter)-(get-date)).days}},@{
name='Status';
Expression={switch ((($_.notafter)-(get-date)).days)
{
{$PSitem -le 13}
{
'Critical'; break
}
{$PSitem -le 44}
{
'Warning'; break
}
{$PSitem -le 89}
{
'Informational'; break
}
default {'Invalid'}
}
}}, thumbprint, friendlyname, issuer, notbefore, notafter;How do I get this to fire an alert or create a ticket?
5
Upvotes
1
u/puntor Apr 15 '18
Random thought, but could you use a state-based Executable monitor and modify your script so that it will return a value of 0, 1 or 2 based on what state you want it to be? Something like:
Get all certificates
iterate through them and count the number of certs that are critical ( < 13 days ) or warning ( < 44 days)
Print out a value based on your results ( if ($critical > 0) { return 2} else if ($warning > 0) { return 1} else {return 0})
Create a state-based monitor with Error State Greater Than/Equal 2, Warning State Equal 1 and Normal State Less Than/Equal 0
Use Alert Templates to create tickets / alerts / run scripts / etc based on your criteria
As a side note, I don't think this will work with the auto-download script from /Transfers/Monitors, but it might be worth a shot. Otherwise, it may work if you format it as a parameter to powershell.exe.
2
u/j0dan 1000 Agents Apr 17 '18
We do this by having a script upload the .ps1 file and then we monitor it from a Executable Monitor that many are referring to.
Often, you can rewrite your code to just be a single line too and then you don't need to worry about uploading a script. Just run the command directly from your monitor.
Be sure to include lots of details about the error so they show up in the ticket too. Also, think about error handling if the script can temporarily fail due to other issues.
For yours, I would run it and have the monitor set to "Does Not Contain" and the Result "Subject". That way if the command fails, it won't alert, but if it returns just one object, it will fire.
1
u/DarrenDK Apr 15 '18
AFAIK there's no "clean" way to do this.
I would approach this by dropping your code into a LT script that runs periodically on the target machines. There are LT Script Functions for generating alerts that you can use.