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 23rd, 2010 2:27am

I think the issue is that when you run a script using PowerShell.exe, it's interpreting the command line parameters differently than if you ran it within the PowerShell environment. You should put single quotes around your parameter values (if they might contain spaces) and no other quotes around anything.

So your activity will look like this:

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

 

Here's an example: I have a script called test.ps1 that contains the following:

     PARAM([String] $param1 = $null)
     $blah = $param1
     Write-Host $blah

When I run the script using the following command line:  powershell.exe test.ps1 "This is a test"

I get the following returned:  "This"

When I run the script using the following command line:  powershell.exe test.ps1 -param1 "This is a test"

I get the following returned:  "This"

When I run the script using the following command line:  powershell.exe test.ps1 -param1 'This is a test'

I get the following returned:  "This is a test"

However, when I run the same commands inside the PowerShell environment, I get this output every time:

"This is a test"

Finally I put the following in Opalis into the Run Command activity and it worked:

Program path:   C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
Parameters:      C:\test\ps1 -param1 'This is a test'

 

Free Windows Admin Tool Kit Click here and download it now
November 23rd, 2010 9:29pm

Thanks Robert, that seems to work however Opalis continues to execute my script and does not move on to the next step.  Under "Advanced" tab I have "Wait for the completion of the program" but how do I tell Opalis the program is complete?  It doesn't seem to recognize that on it's own.
November 23rd, 2010 10:12pm

Hmm. Not sure. My PowerShell script runs and exits and I get the object completed with my output in the "PureResults" property.

Do you know what is happening with the script when it just sits there? Is the script stopped? Has it terminated without an exception? Has it terminated with an exception?

I prefer to use the Run .NET Script activity over the Run Program activity because it runs everything in the right environment and everything happens as you expect it to happen.

Free Windows Admin Tool Kit Click here and download it now
November 23rd, 2010 10:53pm

The script has stopped, because when I try running the exact same script via command prompt like this, it terminates fine once it's done.

C:\Documents and Settings\usopalissvc>C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe E:\OpalisScripts\LogArchive\LogArchive_v4.ps1 -MyServer 'USHER6604' -UsingParams '1'

I can see the powershell.exe process spool up, cpu usage fluctuates up and down, and then it dies.

But when I run the equivalent script via Opalis the powershell.exe process spools up, cpu usage goes up and down, then cpu goes to 0% and it just sits there, process still running idle...

I see the following in the program output once the timeout finally expires, and the powershell.exe is forced to terminate.


Stopping C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe (PID 5708) on USHER6378...
Failed to stop remote process
Obtaining the remote execution status...
Disconnecting from USHER6378...
Disconnected
Return value: 1; Log status: 4 (Process terminated on USHER6378 (time elapsed).)

 

Any idea?  Thanks.

November 24th, 2010 7:31pm

I would suggest using either the RETURN command or EXIT command at the end of the script. Perhaps the code used to run the script is waiting for some definitive sign the script is done.
Free Windows Admin Tool Kit Click here and download it now
December 1st, 2010 4:01am

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.


January 27th, 2011 1:51am

Hey Rerizvi,

I have created a scheduled tasks integration pack (well the code for it, havn't release a OIP file but there is a .dll you can play with, I will probably release a OIP file tomorrow) that may help you with this problem.  The scheduled tasks IP allows for a number of actions including creating new scheduled tasks, modifying existing scheuled tasks, running scheduled tasks on demand and getting the status & exit code of the scheduled task. 

For example I use these objects in my Virtual Desktop Deployment WF.  The first thing the WF does is check to see if a powershell script exists on a file share, if not it creates it.  The WF then checks a remote server (a vmware view broker in this case) to see if it has a scheduled task setup, if not it creates one, if it does it modifies it and changes the parameters (the script takes 2 parameters unique to each run, the name of a virtual desktop and the name of a vmware virtual desktop pool to add the new machine too) to be relevant for the current run.  It then starts the scheduled task and waits for it to complete, if it runs over the designated time it kills the scheduled task.

The project is out on codeplex at http://opalisscheduledtasks.codeplex.com/.  Again there is only a .DLL file out there (you can play with it by using the invoke .net object from the QIK category) but I will try to upload a .OIP file tomorrow when I am in the office.

Free Windows Admin Tool Kit Click here and download it now
January 27th, 2011 9:17am

I have released a OIP file
January 27th, 2011 6:14pm

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?

Free Windows Admin Tool Kit Click here and download it now
February 16th, 2011 11:43pm

Charles Joy's created a Integration Pack for PowerShell Script Execution the other month that might be useful, more on his blog.

 

June 9th, 2011 2:22pm

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;


Free Windows Admin Tool Kit Click here and download it now
August 8th, 2011 10:59pm

You could also make a quick assembly using the QIK CLI.  I use this for some quick powershell, using "Run Program", "powershell.exe" in the program, and using a "–command" followed by your script and parameters in the command line section.  Only downside is you have to manage the PS scripts on the action servers that will be running the script. 
September 13th, 2011 10:18pm

You are a lifesaver! I spent two entire days trying to force my ps script to exit & return to Opalis, and this finally did it!
Free Windows Admin Tool Kit Click here and download it now
April 13th, 2012 9:35pm

I have experienced that this method of closing the process does not always work.

but i have found another one:

http://blog.coretech.dk/jgs/sco-2012-running-powershell-scripts-via-run-program-activity/

April 12th, 2013 3:30pm

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

Other recent topics Other recent topics