I have a powershell process step to run a series of scripts in a directory using invoke-sqlcmd as below.
The idea is to allow all scripts to run and fail the step if one failed.
On the target machine the powershell (variables expanded) it runs fine.
However when run in Octopus $lastexitcode does not appear to work properly after the invoke-sqlcmd line.
$exitcode = $lastexitcode does not set $exitcode to anything, hence every script logs as failed even when they ran properly.
Also the logged error has no value for $exitcode.
Any help would be greatly appreciated.
Run .sql scripts from SQL Views folder
foreach ($f in Get-ChildItem -path “#{PathWKSQLViews}” -Filter *.sql | sort-object)
{
“r
nRunning script " + $f.name.split(”.")[0] >> $logfile
invoke-sqlcmd -InputFile $f.fullname -Verbose -OutputSqlErrors $true 4>&1 2>&1 >> $logfile
$exitcode = $lastexitcode
if ( $exitcode -ne 0 )
{
# allow later commands to run, but note error for exit
$failed = $true
# note error
"Error Running script " + $f.fullname + “r
nExitCode was $exitcode” >> $logfile
}
else
{
“Script completed OK.r
n” >> $logfile
}
}
return failure if any script failed
if ( $failed )
{ exit (1) }
else
{
exit (0)
}