question

Bixby-1953 avatar image
0 Votes"
Bixby-1953 asked AliBurakGenc answered

is there a way to alert in SCOM if a PS script was not able to run?

We are using SCOM 2019
we created a rule in SCOM that runs a powershell script to check the age of a file. the script is working fine.
is there a way to create an alert in SCOM to alert us if the following happens:

  • Alert us if the PS script was not able to run

OR

  • Alert us if the file we are monitoring was not found in folder.

I'm not sure how to accomplish this. any assistance or guidance is appreciated

thank you



msc-operations-manager
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Bixby-1953 , How's everything going? I am writing to see if there's any update on our issue. If yes, feel free to let us know.

0 Votes 0 ·
CyrAz avatar image
0 Votes"
CyrAz answered CyrAz commented

When you say "PS script was not able to run", do you mean "it didn't start at all" or "it failed at some point while running"?

Alert in case of file not found in folder : should be fairly easy, but depends on how you wrote the script in the first place. Could you show us?

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@CyrAz

Hi CyrAz,

Thanks for the reply!

Yes, I mean that "it failed at some point while running". I'm curious if there is a way to let me know if there is anything wrong with the script?

Here is a sample of the script I wrote:


 $fullPath = "C:\Windows\System32\TestFile.txt"
 $lastWrite = (get-item $fullPath).LastWriteTime
 $timespan = new-timespan -hours 26 
    
 if (((get-date) - $lastWrite) -gt $timespan) 
 {
 Write-EventLog -LogName "Application" -Source "SCOM" -EntryType "Error" -EventID 9926 -Message "Test  File is over 26 hours old"
    
 }






0 Votes 0 ·

If you use native powershell probeaction to run your script and if your script "fails properly" (if it raises an error), I believe it should be automatically catched and alerted by the "Powershell script failed to run" native rule.

It's still hard to know how your rule works with this short sample... Do you generate a property bag later in the script? If yes, how is it handled in the workflow to trigger the alert?
Or do you have another rule to catch the event and trigger the rule?

0 Votes 0 ·
Crystal-MSFT avatar image
0 Votes"
Crystal-MSFT answered

@Bixby-1953, To check if the file is there, we can use the following PowerShell commands to check:

Here is an example from my test for the reference:
Note: change the information with the one in your environment.

 $file = 'c:\test\filename.txt'
 if (Test-Path -Path $file -PathType Leaf) {
 $test=1
 }
 else
 {
 $test=0
 }

In rule script, we can use $propertyBag to transfer the Stest value and use this parameter as another condition to consider the unhealthy scenario.

Hope it can help.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AliBurakGenc avatar image
0 Votes"
AliBurakGenc answered

If the action to be taken on each case ( file being old, file not found and script was not able to run ) is in the responsibility of the same team who receive the notification, a three state monitor can be used where the real error be Critical and the others be warning.

If three-state is not possible or not prefered, a property can be used like ;

Alert - File Old
Alert - File not found
Alert - Script failed to run (or exception)

and the condition can be set to Contains "Alert".

Regards
Ali

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.