Powershell exit code

  • 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?

  • 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!!!

  • 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.

  • You are most welcome.

    Nice example of the use of PowerShell.

    Gaz

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

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply