christopher.harris

Problems using nexec to call PowerShell script

Discussion created by christopher.harris on Aug 28, 2009
Latest reply on Sep 3, 2009 by christopher.harris
I have a lot of monitoring scripts written in PowerShell that I need to have Nimbus run.

I'm using the nexec probe and what I would like to do is set up a profile something like this:
Command: Powershell.exe -nonInteractive -noProfile -noLogo
Arguments: -command "& {c:\tools\scripts\nimbus\test.ps1 -exitcode 0}"

However, when I run this from the Start tab, it waits for the 5 second timeout and then exits with:
Return code=1
And there is no output in the Std. out or Std. err text boxes.

If I call the script from a command prompt it shows the current date/time, writes the date/time to the file and exits. Just what it's supposed to do.

The script (test.ps1) looks like this:
Param(
    $exitCode
)
get-date
get-date | out-file c:\temp\date.txt -append
If ($exitCode){$host.SetShouldExit($exitCode)}








If I configure the nexec profile like this:
Command: cmd.exe /c
Arguments: Powershell.exe -nonInteractive -noProfile -noLogo -command "& {c:\tools\scripts\nimbus\test.ps1 -exitcode 0}"

The script will add the current date/time to the date.txt file, but does not return the date/time to the std. out text box (though like I said if I call from a command prompt this works properly), and it waits the full 5 seconds before timing out (but does not wait if I run the same command from a command prompt). It waits the full timeout regardless of whether I call $host.SetShouldExit($exitCode) or not. (That statement just tells the host, powershell.exe in this case, to exit with an exit code specified by $exitCode.)


Additionally, if I wrap the call to the powershell script in a batch file the date.txt file is updated, but the date/time is not returned to std. out. However in this case,  I do get some std. out:
Calling PowerShell

powershell.exe -command "& {c:\tools\scripts\nimbus\test.ps1 -exitcode 0}" -nonInteractive -noProfile -noLogo
Two things I'm still not getting are the date and the final @Echo Done Calling Powershell.


Here is the configuration for this profile:
Command: cmd.exe /c
Arguments: c:\tools\scripts\nimbus\test.cmd
Contents of test.cmd:
@Echo Calling PowerShell
powershell.exe -command "& {c:\tools\scripts\nimbus\test.ps1 -exitcode 0}" -nonInteractive -noProfile -noLogo
@Echo Done Calling Powershell


If anyone has been through this, or just has some brilliant ideas, I would be most grateful!

Thanks,
Chris

Outcomes