Question

I'm working on a deployment script for a vendor application. It reads an xml that maps the deploy paths for the various environments, then iterates over the various servers and copies the correct files over and executes the rollout script. I would like the script to exit if the rollout script fails. The rollout script is a perl script and I need to precede that by calling a .bat to set a bunch of environment variables. I've issued all of those commands strung together with &'s as you can see below so that that system variables that env.bat sets are still available when the perl script runs:

$cmdString = $destDrive + ": & call "+$lesDestDir+"data\env.bat & cd "+$rolloutDir+" & perl ..\JDH-rollout-2010.pl "+$rollout+" NC,r:\les"
write-host "cmdString: " $cmdString
Invoke-Command -session $session -ScriptBlock {cmd /v /k `"$args[0]`"} -args $cmdString

The invoke command would look like this with variable substitution:

Invoke-Command -session $session -scriptblock {cmd /k "D: & call D:\XXXX\env.bat & cd D:\XXXX\RO-00102 & perl ..\JDH-rollout-2010.pl RO-00102 NC,r:\les"}

So the question is how can I get the return code from the perl script (which is the last command in a string of commands running in the context of cmd) back to the local machine where the script is running? Any other suggestions towards my approach are welcome as I have limited powershell experience.

Was it helpful?

Solution

I found the solution shortly after. I added an echo of the return code to my $cmdString, then used invoke-command to return the remote variable back to the local instance, then I interrogated the results for the return code:

$cmdString = $destDrive + ": & call "+$lesDestDir+"data\env.bat & cd "+$rolloutDir+" & perl ..\JDH-rollout-2010.pl "+$rollout+" NC,r:\les & echo ExitCode:!errorlevel!"
write-host "cmdString: " $cmdString

Invoke-Command -session $session -ScriptBlock {$res = cmd /v /k `"$args[0]`"} -args $cmdString
$res = Invoke-Command -session $session {$res}    
Write-Host "Return code: " $res    
$res = [string] $res        
$exitCode = $res.substring($res.IndexOf("ExitCode:"), 10)     
Write-Host "Exit code: " $exitCode
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top