$error[0].tostring().contains not working

I have the following try-catch statement to process a DB table/dimension. For some reason, if i turn off the (MSSQLSERVER) service on the server, the 1st if statement to capture the exception that

Ensure that the server is running
is never touched and instead it goes to the second if condition and writes to host
Error Processing dimension: A connection cannot be made. Ensure that the server is running.
        try {
                Invoke-ProcessTable -DatabaseName "$DB" -Server "$server" -RefreshType "Full" -TableName "sometable" > $null
            write</span><span class="pun">-</span><span class="pln">host </span><span class="str">"Processing completed!"</span>
    <span class="pun">}</span><span class="pln">
    catch</span><span class="pun">{</span>
            <span class="kwd">if</span><span class="pun">(</span><span class="pln">$error</span><span class="pun">[</span><span class="lit">0</span><span class="pun">].</span><span class="pln">tostring</span><span class="pun">().</span><span class="pln">contains</span><span class="pun">(</span><span class="str">"Ensure that the server is running"</span><span class="pun">))</span>
            <span class="pun">{</span><span class="pln">
                write</span><span class="pun">-</span><span class="pln">host </span><span class="str">"&#8175;r&#8175;nProcessing failed!&#8175;r&#8175;n(MSSQLSERVER) service is not running on $server!"</span>
                <span class="com">#$exit = 1</span>
            <span class="pun">}</span><span class="pln">
            elseif </span><span class="pun">(</span><span class="pln">$error</span><span class="pun">[</span><span class="lit">0</span><span class="pun">])</span>
            <span class="pun">{</span>
                <span class="typ">Write</span><span class="pun">-</span><span class="typ">Host</span> <span class="str">"Error Processing dimension: $($error[0])"</span>
            <span class="pun">}</span>
    <span class="pun">}</span></code></pre>

The exception message if the service is not running on the server is as follows:

A connection cannot be made. Ensure that the server is running.
so what is wrong with $error[0].tostring().contains("Ensure that the server is running")

it should be outputting this:

Processing failed!(MSSQLSERVER) service is not running on server!

Contains is a array search.

PS C:\> 'red', 'yellow', 'blue' -contains 'purple'

False

PS C:\> 'red', 'yellow', 'blue' -contains 'yellow'

True

For a string comparison, you should use -like:

PS C:\> 'A connection cannot be made. Ensure that the server is running.' -like '*Ensure*'

True

Also, you are not checking the error properly in your try\catch. Using $_ under catch is the current exception (e.g. $_.Exception.Message). You can also look at catching a specific exception. Take a look at the free ebook on the left The Big Book of PowerShell Error Handling

[quote quote=139083][/quote]
nope, -like doesnt help either…

@Rob: for the -contains operator you’re right. The .contains() string method works actually as the OP expect it. :wink:

Try it:

‘A connection cannot be made. Ensure that the server is running.’.contains(“Ensure that the server is running”)

just tried $error[0].contains(“Ensure that the server is running”)

doesnt work :confused:

[quote quote=139090]just tried $error[0].contains(“Ensure that the server is running”)

doesnt work :/[/quote]

So your $error[0] might not contain what you expect it to contain. Did you check this?

[quote quote=139092][/quote]
If it doesnt contain that, then how is it possible that this

Write-Host "Error Processing dimension: $($error[0])"

writes:

Error Processing dimension: A connection cannot be made. Ensure that the server is running.

That means that $error[0] definitely holds this string content:

A connection cannot be made. Ensure that the server is running.

 

Did you see/check/try the answer from James in the Microsoft forum:
https://social.technet.microsoft.com/Forums/en-US/eaa354a8-4880-4707-af5e-703513e2968c/why-error0tostringcontains-not-working-correctly?forum=winserverpowershell#52677577-8896-415c-b5b7-009c5f1041a4

PS C:\> ('A connection cannot be made. Ensure that the server is running.').contains('Ensure')

True

PS C:\> ('A connection cannot be made. Ensure that the server is running.').contains('Rob')

False

@Olaf, never used it that way, but it does work.

@cataster16

$error[0] is an object, not a string. As I mentioned earlier, you should be using $_ to represent the current exception. Also, you need to add -ErrorAction Stop to the command. If you want to test the message, it should be:

try {
    Invoke-ProcessTable -DatabaseName "$DB" -Server "$server" -RefreshType "Full" -TableName "sometable" -ErrorAction Stop > $null
    write-host "Processing completed!"
}
catch{
        if ($_.Exception.Message.contains("Ensure that the server is running")) {
            write-host "`r`nProcessing failed!`r`n(MSSQLSERVER) service is not running on $server!"
        }

}

thanks everyone. with $_ i was able to debug the real issue. $error[0] does work but i was not paying attention to the correct try-catch block

Give this a shot:

try
{
Invoke-ProcessTable -DatabaseName "$DB" -Server "$server" -RefreshType "Full" -TableName "sometable" > $null

write-host "Processing completed!"
}

catch [EnterTypeNameOfException]
{
if($Error[0].Exception.Message.Contains("Ensure that the server is running")) {
write-host "`r`nProcessing failed!`r`n(MSSQLSERVER) service is not running on $server!"
}

}