why is the try catch block not working correctly with errors

0

I am trying to capture errors in a meaningful way, as thats my habit, and when it comes to this block of code, i am not getting the result/output im hoping for:

Unblock-File $dll1 -Verbose 4>&1

Add-Type -Path $dll1
Add-Type -Path $dll2

I get this output:

ERROR! Could not load file or assembly 'file:///C:\Users\Documents\DLLS\Microsoft.AnalysisServices.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) At C:\Users\Documents\test.ps1:7 char:2 + Add-Type -Path $dll1 + ~~~~~~~~~~~~~~~~~~~~ at , C:\Users\Documents\test.ps1: line 7

This error typically occurs when files are downloaded from a potentially untrusted source and therefore are blocked.

Attempting to Unblock file(s)...

VERBOSE: Performing the operation "Unblock-File" on target "C:\Users\Documents\DLLS\Microsoft.AnalysisServices.dll".

ERROR! Could not load file or assembly 'file:///C:\Users\Documents\DLLS\Microsoft.AnalysisServices.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) At C:\Users\Documents\test.ps1:24 char:2 + Add-Type -Path $dll1 + ~~~~~~~~~~~~~~~~~~~~ at , C:\Users\Documents\test.ps1: line 24

After the VERBOSE, this shouldn't error out again:

ERROR! Could not load file or assembly 'file:///C:\Users\Documents\DLLS\Microsoft.AnalysisServices.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) At C:\Users\Documents\test.ps1:24 char:2 + Add-Type -Path $dll1 + ~~~~~~~~~~~~~~~~~~~~ at , C:\Users\Documents\test.ps1: line 24

instead, now that the file is unblocked, the assembly should be loaded with the second attempt in this 2nd try block. Add-Type -Path $dll1. so why is it outputting the error once again? it should output it for the second dll, because that one has yet to be unblocked, but instead it still complains about dll1. I can see that dll1 file was truly unblocked.

Here is my full code:

$dllPath = "C:\Users\Documents\DLLS"

$dll1 = "$dllPath\Microsoft.AnalysisServices.dll"
$dll2 = "$dllPath\Microsoft.AnalysisServices.Core.dll"

try {
    Add-Type -Path $dll1
    Add-Type -Path $dll2
}
catch {
    if ($error[0].tostring().contains("Operation is not supported. (Exception from HRESULT: 0x80131515)"))
    {
        write-host "`r`nERROR! $($error[0])`r`n$($error[0].InvocationInfo.PositionMessage)`r`n$($error[0].ScriptStackTrace)" -foregroundcolor red -backgroundcolor black
        write-host "`r`nThis error typically occurs when files are downloaded from a potentially untrusted source and therefore are blocked." -foregroundcolor magenta -backgroundcolor black
        write-host "`r`n Attempting to Unblock file(s)...`r`n" -foregroundcolor cyan -backgroundcolor black

        $error.clear()

        try {
            #Get-ChildItem -Path $dllPath\*.* -Filter *.dll | Unblock-File

            Unblock-File $dll1 -Verbose 4>&1

            Add-Type -Path $dll1
            Add-Type -Path $dll2
        }
        catch {
            write-host "`r`nERROR! $($error[0])`r`n$($error[0].InvocationInfo.PositionMessage)`r`n$($error[0].ScriptStackTrace)" -foregroundcolor red -backgroundcolor black
        }
    }
    else
    {
        write-host "`r`nERROR! $($error[0])`r`n$($error[0].InvocationInfo.PositionMessage)`r`n$($error[0].ScriptStackTrace)" -foregroundcolor red -backgroundcolor black
    }
}
powershell
asked on Stack Overflow Jan 22, 2020 by Cataster

1 Answer

0

Add this line of code to the top suppress error notifications.

$ErrorActionPreference = 'SilentlyContinue'
answered on Stack Overflow Jan 23, 2020 by LogRhythm

User contributions licensed under CC BY-SA 3.0