This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Execute An External Program - Pass node name to powershell script

Hi.

I want to use the Execute An External Program as a Trigger Action on an alert.  I want that to call a powershell script, but i also want to pass the Server Hostname generating the alert to the powershell script.  Has anyone been successful and if so can you please explain how to do this.  Screenshot below is an example of what i thought after reading other pages i could do.   I am doubting the variable i call out is correct, or is that even the correct method to pass it?

camping1981_0-1611233652354.png

Also when you do get the name to pass corrrectly, how do you then get that parameter in your script.  What i read suggests if you pass 1 parameter it would be $args[0] so something like

$hostname = $args[0]

Can anyone please help me with this.  I have taken the live trainings and asked in there.  Seems my question was out of scope for those trainings, or the trainers don't work with powershell or doing this type of action much.  

Thanks in advance. 

I'll also include 1 use case here.  I only use SAM for server monitoring.  The OOTB Node Rebooted is generic.   Email alert says "Hostnmae rebooted".  I want to use a Powershell script to collect the event from the host for the reboot and the message stating who/why the server rebooted.  so like this.  

$server = "$args[0]"
$info = Get-WinEvent -ComputerName $server -FilterHashtable @{Logname='System';ID=1074} -MaxEvents 1
$time = $info.timecreated
$message = $info.message
$body = "Server $server has rebooted at $time, `n $message "

then use Send-Mailmessage to email out the alert. 

  • I'm going to answer my own question here because i figured it out.  

    What i was trying in the "execute an external program" on the line "Network path to external program" was putting the path to myscript.

    So i was trying like this

    //Servename/D$/scripts/myscript.ps1 ${NodeName}

    I found that did not work.  What does work is the following.  In you "Network Path to external Program" you need to call out powershell.exe followed by file in quotes and your variable then in quotes such as.  

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "D:\scripts\myscript.ps1" "${NodeName}"

    Your parameters are passed into the script as arguments.  So for nodename i can get it by the following in my ps1 script

    $hostname = $args[0]

    Each additional parameter you want to pass would need in it's own quotes, and you can then call them in order $args[1], $args[2], etc....

    I hope this helps someone else.