Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Powershell exit code Expand / Collapse
Author
Message
Posted Thursday, July 3, 2014 9:05 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: 2 days ago @ 6:54 PM
Points: 99, Visits: 763
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?
Post #1588977
Posted Thursday, July 3, 2014 9:37 AM This worked for the OP Answer marked as solution


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:52 AM
Points: 5,814, Visits: 3,735
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!!!
Post #1589010
Posted Thursday, July 3, 2014 1:39 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: 2 days ago @ 6:54 PM
Points: 99, Visits: 763
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.
Post #1589119
Posted Thursday, July 3, 2014 1:49 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:52 AM
Points: 5,814, Visits: 3,735
You are most welcome.

Nice example of the use of PowerShell.


Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Post #1589125
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse