The correct way to call an external PowerShell script (using Run Program object)

I'm trying to figure out the correct way to call a PowerShell script outside of Opalis.  I've had soOo much trouble trying to run PowerShell scripts within the Opalis "Run .Net Code" object (scripts that otherwise work fine outside of Opalis) that I figured it would just be easier to write my PoSH script and run it by passing some parameters from Opalis.  Turns out even THIS is not as easy as I would expect. :)

Here is what I have tried (all using the "Run Program" object):

Computer: localhost
Program path: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe "E:\OpalisScripts\recycle_app_pools.ps1 -Computer {NetbiosComputerName from Monitor SCOM Alert} -BadPool {MonitoringObjectName from Monitor SCOM Alert}"

Parameters:
Working Folder: E:\OpalisScripts

Computer: localhost
Program path: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe "E:\OpalisScripts\recycle_app_pools.ps1"

Parameters: -
Computer {NetbiosComputerName from
 Monitor SCOM Alert} -
BadPool {MonitoringObjectName from
 Monitor SCOM Alert}
Working Folder: E:\OpalisScripts

Computer: localhost
Program path: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe E:\OpalisScripts\recycle_app_pools.ps1
Parameters: -
Computer {NetbiosComputerName from
 Monitor SCOM Alert} -
BadPool {MonitoringObjectName from
 Monitor SCOM Alert}
Working Folder: E:\OpalisScripts

Computer: localhost
Program path: powershell.exe E:\OpalisScripts\recycle_app_pools.ps1
Parameters: -
Computer {NetbiosComputerName from
 Monitor SCOM Alert} -
BadPool {MonitoringObjectName from
 Monitor SCOM Alert}
Working Folder: E:\OpalisScripts

 

None of these work, I have no idea what it wants... any suggestions? :(

November 22nd, 2010 11:27pm

Hi Robert.  I was hoping you could help me figure out this issue with running PowerShell scripts using the Run Program object, because I am still faced with this same challenge.  As a work around I have been using the "Do not wait for completion of the program" and built logic in my Opalis policy to wait some time so I can be sure the script completed.  But I now have a need to actually execute some logic right after the program has completed, but this is not working.

I tried writing a very simple script (Write-Host "Hello World") but I'm getting the same result --  The powershell.exe process keeps running and doesn't terminate until the timeout expires.

- I have tried both program and command execution methods.

- Tried both Interactive and Background modes.

- If I use the "Wait for completion.." mode it will keep running the powershell.exe process until the defined time limit is reached.

- I tried adding a "return" and "exit" at the end of the script, with no luck.

I am using Opalis 6.2.2.5229 if that makes any difference... maybe this bug is fixed in later versions?

Any help is greatly appreciated.


 

Thanks Ryan, the scheduled task IP looks great, I think we can definitely use that. 

But still the issue with the never ending PowerShell scripts is still happening, any time I try using that object.  Anyone having this same issue, or just me?

  • Proposed as answer by Andy Dinger Tuesday, September 13, 2011 7:14 PM
  • Unproposed as answer by Andy Dinger Tuesday, September 13, 2011 7:14 PM
Free Windows Admin Tool Kit Click here and download it now
February 16th, 2011 8:43pm

Simply use the standard 'Run Program' object and add the following two lines to the end of your PS script.  This forces the closure of the current process.  Opalis captures the output as 'Pure Output' as if the process had terminated gracefully on its own.

$objCurrentPSProcess = [System.Diagnostics.Process]::GetCurrentProcess();
Stop-Process -Id $objCurrentPSProcess.ID;


  • Proposed as answer by D Burton Monday, August 08, 2011 8:21 PM
  • Edited by D Burton Tuesday, September 06, 2011 3:49 PM
August 8th, 2011 7:59pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics