SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Powershell exit code


Powershell exit code

Author
Message
sqlpadawan_1
sqlpadawan_1
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 1052
I have a script that I'm trying to run in TeamCity, essentially I want to force an exit code if an error happens.

Here's the script:

$s = "kbullen-865";
$db_name = "tempdb";
$ExceptionMessage = "";
##Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$serv = New-Object Microsoft.SqlServer.Management.SMO.Server($s)

Function CheckObject
{
Param ( $DatabaseName, $ObjectSchema, $ObjectName)
$scon = New-Object System.Data.SqlClient.SqlConnection
#$scon.ConnectionString = "SERVER=" + $s + ";DATABASE=" + $DatabaseName + ";Integrated Security=true"
$scon.ConnectionString = "SERVER=$s;DATABASE=$db_name;Integrated Security=true"
$trycmd = New-Object System.Data.SqlClient.SqlCommand

## We refresh the object with the schema.table
$trycmd.CommandText = "EXECUTE sys.sp_refreshsqlmodule '$ObjectSchema.$ObjectName'"
$trycmd.Connection = $scon
try
{
$scon.Open()
$trycmd.ExecuteNonQuery() | Out-Null
}
catch [Exception]
{
$CurrentException = $_.Exception.Message -replace "'", "''";
$ExceptionMessage = $ExceptionMessage + $CurrentException;
#Write-Host $DatabaseName"."$ObjectSchema"."$ObjectName ":"
#Write-Host $_.Exception.Message
Write-Host $CurrentException;
}
finally
{
$scon.Close()
$scon.Dispose()
}
}

$db = $serv.Databases[$db_name];

foreach ($proc in $db.StoredProcedures | Where-Object {$_.IsSystemObject -eq $false})
{
$o = $proc.Name
$sc = $proc.Schema
CheckObject $db.Name $sc $o
}
foreach ($trig in $db.Triggers | Where-Object {$_.IsSystemObject -eq $false})
{
$o = $trig.Name
$sc = $trig.Schema
CheckObject $db.Name $sc $o
}
foreach ($udf in $db.UserDefinedFunctions | Where-Object {$_.IsSystemObject -eq $false})
{
$o = $udf.Name
$sc = $udf.Schema
CheckObject $db.Name $sc $o
}
foreach ($view in $db.Views | Where-Object {$_.IsSystemObject -eq $false})
{
$o = $view.Name
$sc = $view.Schema
CheckObject $db.Name $sc $o
}

if ($ExceptionMessage)
{
Write-Host $ExceptionMessage;
exit 1;
}



The code appears to work, I get values written to the host by the line Write-Host $CurrentException. But at the end, the $ExceptionMessage appears empty.

What am I doing wrong?
Gary Varga
Gary Varga
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27779 Visits: 6557
It is all about scope. $ExceptionMessage in the function is not the same as the variable in the scope of the script. See this script:


$stringValue = "global"

Function F
{
$stringValue = "F"
Write-Host $stringValue # Outputs "F"
}

Write-Host $stringValue # Outputs "global"
F
Write-Host $stringValue # Outputs "global"



You could:
a) Write-Host in the function.
b) Return the exception (or its text) from the function.
c) Move the exception handling out of the function (probably not what you want as it would not check each item in your loops).

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
sqlpadawan_1
sqlpadawan_1
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 1052
Perfect! Thanks.

Here's the final scripts

$s = "kbullen-865";
$db_name = "tempdb";
$CombinedExceptionMessage = "";
##Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$serv = New-Object Microsoft.SqlServer.Management.SMO.Server($s)

Function CheckObject
{

Param ($DatabaseName, $ObjectSchema, $ObjectName)
$scon = New-Object System.Data.SqlClient.SqlConnection
#$scon.ConnectionString = "SERVER=" + $s + ";DATABASE=" + $DatabaseName + ";Integrated Security=true"
$scon.ConnectionString = "SERVER=$s;DATABASE=$db_name;Integrated Security=true"
$trycmd = New-Object System.Data.SqlClient.SqlCommand

## We refresh the object with the schema.table
$trycmd.CommandText = "EXECUTE sys.sp_refreshsqlmodule '$ObjectSchema.$ObjectName'"
$trycmd.Connection = $scon
try
{
$scon.Open()
$trycmd.ExecuteNonQuery() | Out-Null
}
catch [Exception]
{
$CurrentException = $_.Exception.Message -replace "'", "''";
#$ExceptionMessage = $ExceptionMessage + $CurrentException;
#Write-Host $DatabaseName"."$ObjectSchema"."$ObjectName ":"
#Write-Host $_.Exception.Message
#Write-Host $CurrentException;
}
finally
{
$scon.Close()
$scon.Dispose()
}

Return $CurrentException;
}

$db = $serv.Databases[$db_name];

$CombinedExceptionMessage = "";

foreach ($proc in $db.StoredProcedures | Where-Object {$_.IsSystemObject -eq $false})
{
$NewExceptionMessage = "";
$o = $proc.Name
$sc = $proc.Schema
if ($sc -ne "tSQLt" -and $sc -ne "SQLCop")
{
$NewExceptionMessage = CheckObject $db.Name $sc $o
if ($NewExceptionMessage)
{
$CombinedExceptionMessage = $CombinedExceptionMessage + $db.Name + "." + $sc + "." + $o + ": ";
$CombinedExceptionMessage = $CombinedExceptionMessage + $NewExceptionMessage.replace("`n","").replace("`r","") + "`r`n";
$NewExceptionMessage = "";
}
}
}

foreach ($trig in $db.Triggers | Where-Object {$_.IsSystemObject -eq $false})
{
$NewExceptionMessage = "";
$o = $trig.Name
$sc = $trig.Schema
$NewExceptionMessage = CheckObject $db.Name $sc $o
if ($sc -ne "tSQLt" -and $sc -ne "SQLCop")
{
$NewExceptionMessage = CheckObject $db.Name $sc $o
if ($NewExceptionMessage)
{
$CombinedExceptionMessage = $CombinedExceptionMessage + $db.Name + "." + $sc + "." + $o + ": ";
$CombinedExceptionMessage = $CombinedExceptionMessage + $NewExceptionMessage.replace("`n","").replace("`r","") + "`r`n";
$NewExceptionMessage = "";
}
}
}

foreach ($udf in $db.UserDefinedFunctions | Where-Object {$_.IsSystemObject -eq $false})
{
$NewExceptionMessage = "";
$o = $udf.Name
$sc = $udf.Schema
$NewExceptionMessage = CheckObject $db.Name $sc $o
if ($sc -ne "tSQLt" -and $sc -ne "SQLCop")
{
$NewExceptionMessage = CheckObject $db.Name $sc $o
if ($NewExceptionMessage)
{
$CombinedExceptionMessage = $CombinedExceptionMessage + $db.Name + "." + $sc + "." + $o + ": ";
$CombinedExceptionMessage = $CombinedExceptionMessage + $NewExceptionMessage.replace("`n","").replace("`r","") + "`r`n";
$NewExceptionMessage = "";
}
}
}

foreach ($view in $db.Views | Where-Object {$_.IsSystemObject -eq $false})
{
$NewExceptionMessage = "";
$o = $view.Name
$sc = $view.Schema
$NewExceptionMessage = CheckObject $db.Name $sc $o
if ($sc -ne "tSQLt" -and $sc -ne "SQLCop")
{
$NewExceptionMessage = CheckObject $db.Name $sc $o
if ($NewExceptionMessage)
{
$CombinedExceptionMessage = $CombinedExceptionMessage + $db.Name + "." + $sc + "." + $o + ": ";
$CombinedExceptionMessage = $CombinedExceptionMessage + $NewExceptionMessage.replace("`n","").replace("`r","") + "`r`n";
$NewExceptionMessage = "";
}
}
}

if ($CombinedExceptionMessage)
{
Write-Host $CombinedExceptionMessage;
exit 1;
}



Probably not the most elegant, but it works.
Gary Varga
Gary Varga
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27779 Visits: 6557
You are most welcome.

Nice example of the use of PowerShell.

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search