I have a problem running a powershell script from within another powershell script, passing parameters and capturing the output. I have tried using the ampersand operator, calling it via
powershell.exe -Command but nothing seems to work.
What seems to work is using fixed parameter and values stored in a variable like this
C:\path\to\script.ps1 -arg1 $value.
This may present a solution if nothing else works, but I would like to run the command similar to this
& $pathToScript $params 2>&1 (the
2>&1is for capturing error output as well as standard).
Sometimes the construct prints just the path to the script,
sometimes it says
Cannot run file in the middle of pipeline and
sometimes it complains about it cannot find the mentioned script file (I sometimes have spaces in my path, but I thought quoting it would suffice: quoting was done like this
$path = ").
This is the simplified function I want to use this in:
#the function receives the script path and parameters
#this works if no params are passed, but I need params!
$output = & $path $params 2>&1 | Out-String
I solved the problem with the help of a colleague.
We went a little indirection and included a
cd into the respective directory and ran the command afterwards. This works like a charm.
Solution source code:
cd "C:\into\path with\space"
$output = & .\$fileName $params 2>&1 | Out-String
This works and even captures the error output, I hope some other folks encountering this kind of problem can use this to fix their problems.
Cheerioh and thanks for reply!