I'm confused:
The following code produces an error:
try {
invoke-sqlcmd -ServerInstance 'localhost' -Database 'tempdb' -Query 'CREATE TABLE foo (bar TINYINT IDENTITY(1,1) DEFAULT 1);;' -Verbose -ErrorLevel 0 -AbortOnError -ErrorAction Stop -OutputSqlErrors $true -ErrorVariable $err -OutVariable $err -SeverityLevel 0;
"OK";
}
catch {
"ERROR"
$_
}
Result:
ERROR Invoke-Sqlcmd : Defaults cannot be created on columns with an IDENTITY attribute. Table 'foo', column 'bar'.
This piece runs apparently smoothly:
try {
invoke-sqlcmd -ServerInstance 'localhost' -Database 'tempdb' -Query 'SELECT CAST(111111111111 AS TinyINT) AS Error' -Verbose -ErrorLevel 0 -AbortOnError -ErrorAction Stop -OutputSqlErrors $true -ErrorVariable $err -OutVariable $err -SeverityLevel 0;
"OK"
}
catch {
"ERROR"
$_
}
Result:
OK
In both cases the catch-block must be used!