Call script from script; exit code issues
i have verified in simple pair of scripts can feed exit code calling script. "parent" test little more than
powershell.exe -file "$scriptpath\child.ps1"
write-host "$lastexitcode!!!"
and child script just
exit 1
and write-host comes correctly. have tried 2 different child scripts, different rerun codes, being called parent back. good.
however, in more complicated script, child script has conditional exits, getting 0 time. conditional looks this
if ($global:memberofset) {
if ($global:erroroccured) {
write-host '1'
exit 1
}
else {
write-host '0'
exit 0
}
}
and when 1 echoes, still $lastexitcode of 0.
is there known issue here not aware of? using ps 2.0, fwiw.
thanks!
um, no, more remmed out [cmdletbinding()] @ top of each child script , no other changes working should. not sure why worked, not imagining it.
for shits , giggles added binding in little test script set, , broken, no other changes. hadn't created test obviously, wasn't expecting binding matter. love hear explanation why would
for wanting see behavior, these 3 test scripts, , run arch script. binding enabled dtv show 1, return 0. rem bindings , dtv will correctly return 1. tested rvt not memberofset, -1 returned, , $lastexitcode shows wonky number expected given bug in 2.0 , negative return codes.
note: in test argument in child scripts not used, it's there avoid error otherwise when binding. in "live" code arguments are mandatory , used.
arch_2015.ps1
powershell.exe -file "rvt_2015.ps1"write-host "$lastexitcode!"
powershell.exe -file "dtv_2015.ps1"
write-host "$lastexitcode!"
dtv_2015.ps1
[cmdletbinding()]param ([string]$mode)
$global:memberofset = $true
$global:erroroccured = $true
if ($global:memberofset) {
if ($global:erroroccured) {
write-host 'dtv 1'
exit 1
}
else {
write-host 'dtv 0'
exit 0
}
}
else {
write-host 'rvt -1'
exit -1
}
rvt_2015.ps1
[cmdletbinding()]param ([string]$mode)
$global:memberofset = $true
$global:erroroccured = $false
if ($global:memberofset) {
if ($global:erroroccured) {
write-host 'rvt 1'
exit 1
}
else {
write-host 'rvt 0'
exit 0
}
}
else {
write-host 'rvt -1'
exit -1
}
Windows Server > Windows PowerShell
Comments
Post a Comment