Error handling within a workflow
I have got a Ps script which loops a list of servers, however the error handling is not behaving the way I would expect it to. The invoke-sqlcmd has been set to continue on error, this is deliberate because when looping through a list of servers, I dont want it to stop every time it encounters an error. On the other hand, I would like to know if there has been an error.
What I opted to do was to set the erroractionpreference to continue within the script, but the invoke-sqlcmd command set to stop. This works well outside of powershell workflows, but within a PS workflow its having undesired effects.
In the example below, I took out the try catch as it was masking errors.
Workflow RunDeployment
{
$file_list = @("deployment_1.sql","deployment_2.sql","deployment_3.sql","deployment_4.sql","deployment_5.sql")
$x = (1,"localhost\DEV2",3,1),(4,"localhost\DEV2",6,2),(3,"localhost\DEV2",4,3)
$k = 'serverid','servername','locationid','appid'
$h = @{}
For($i=0;$i -lt $x[0].length; $i++){
$x |
ForEach-Object{
[array]$h.($k[$i]) += [string]$_[$i]
}
}
$folder = "C:\Temp\"
$database_name = "Test"
$all_server_ids = $h['serverid']
foreach -parallel ($server_id in $all_server_ids)
{
$ErrorActionPreference = 'Continue'
$severid = $h["serverid"][$all_server_ids.indexof($server_id)]
$servername = $h["servername"][$all_server_ids.indexof($server_id)]
$locationid = $h["locationid"][$all_server_ids.indexof($server_id)]
foreach ($file in $file_list)
{
$release_file = "$folder$file"
write-output "The file is $release_file "
# try {
invoke-sqlcmd -ServerInstance "$servername" -inputfile $folder$file -Database "$database_name" -Querytimeout 60 -OutputSqlErrors $true -ConnectionTimeout 10 -ErrorAction Stop -Errorvariable errorvalue
# write-output "-ServerInstance $servername -inputfile $folder$file -Database $database_name -Querytimeout 60 -OutputSqlErrors $true -ConnectionTimeout 10 -ErrorAction Stop -Errorvariable errorvalue"
# if ($errorvalue){
# write-output "Error encountered see $errorvalue" }
# }
# Catch{
# $error_message = $_.Exception.Message
# write-output $error_message
# write-output $error
# }
}
}
}
RunDeployment