none
why is the error outputting again in the try catch block? RRS feed

  • Question

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

    Thursday, January 23, 2020 4:20 PM

Answers

  • Here's a way to search for blocked files.  You could pipe this to remove-item to unblock them. 


    get-item * -stream zone*

    If you are goinf to do it you will have to code it correctly.  Your code will not get anything.

     Get-Childitem ./*.dll | Unblock-File -Verbose


    \_(ツ)_/


    • Edited by jrvModerator Saturday, January 25, 2020 11:59 PM
    • Marked as answer by cataster Tuesday, January 28, 2020 1:38 PM
    Saturday, January 25, 2020 11:58 PM
    Moderator

All replies

  • This is show to use a Try/Catch:

    try {
        Add-Type -Path $dll1 -ErrorAction Stop
        Add-Type -Path $dll2 -ErrorAction Stop
    }
    Catch{
       ....


    \_(ツ)_/

    Thursday, January 23, 2020 5:46 PM
    Moderator
  • This is show to use a Try/Catch:

    try {
        Add-Type -Path $dll1 -ErrorAction Stop
        Add-Type -Path $dll2 -ErrorAction Stop
    }
    Catch{
       ....


    \_(ツ)_/

    nope, still doesnt change the output. 
    Thursday, January 23, 2020 6:16 PM
  • You only fixed one set.  All CmdLets have to be modified.

    This is all you have to do:

    $dllPath = "C:\Users\Documents\DLLS"
    $dll1 = "$dllPath\Microsoft.AnalysisServices.dll"
    $dll2 = "$dllPath\Microsoft.AnalysisServices.Core.dll"
    
    try {
        Unblock-File $dll1,$dll2 -ErrorAction Stop
        Add-Type -Path $dll1 -ErrorAction Stop
        Add-Type -Path $dll2 -ErrorAction Stop
    }
    catch {
        Throw $_ # rethrow exception
    }


    \_(ツ)_/

    Thursday, January 23, 2020 6:22 PM
    Moderator
  • An even cleaner way to do this would be the following:

    try {
        $ErrorActionPreference = 'Stop'
        $dllPath = "C:\Users\Documents\DLLS"
        $dll1 = "$dllPath\Microsoft.AnalysisServices.dll"
        $dll2 = "$dllPath\Microsoft.AnalysisServices.Core.dll"
        Unblock-File $dll1,$dll2
        Add-Type -Path $dll1
        Add-Type -Path $dll2
    }
    catch {
        Throw $_ # rethrow exception
    }
    Finally{
        $ErrorActionPreference = 'SilentlyContinue'
    }


    \_(ツ)_/

    Thursday, January 23, 2020 6:24 PM
    Moderator
  • An even cleaner way to do this would be the following:

    try {
        $ErrorActionPreference = 'Stop'
        $dllPath = "C:\Users\Documents\DLLS"
        $dll1 = "$dllPath\Microsoft.AnalysisServices.dll"
        $dll2 = "$dllPath\Microsoft.AnalysisServices.Core.dll"
        Unblock-File $dll1,$dll2
        Add-Type -Path $dll1
        Add-Type -Path $dll2
    }
    catch {
        Throw $_ # rethrow exception
    }
    Finally{
        $ErrorActionPreference = 'SilentlyContinue'
    }


    \_(ツ)_/

    i could do it that way (or the other way you have above), but i want to output the error first time. thats is why i have 2 try catches. the only thing that it shouldnt be doing is it shouldnt be outputting the same error again that the file has been unblocked in that second/nested try catch block. 

    i like doing it this way because it keeps track of what happened to begin with and what solution was taken by the script (verbose) and if that solution fixed that error or not, and if it did but there was a DIFFERENT error (different from Operation is not supported. (Exception from HRESULT: 0x80131515) ) then, that is where that nested catch block comes in. 

    Thursday, January 23, 2020 7:36 PM
  • You cannot load the assembly because it is either not installed correctly or it is not a correct assembly for you version of Windows.

    The question you asked was answered.  The issue of that DLL is not a scripting issue.  It can be that your folder is not trusted.  This will happen if you have relocated the documents folder to a network location or to the cloud.


    \_(ツ)_/

    Thursday, January 23, 2020 8:00 PM
    Moderator
  • You cannot load the assembly because it is either not installed correctly or it is not a correct assembly for you version of Windows.

    The question you asked was answered.  The issue of that DLL is not a scripting issue.  It can be that your folder is not trusted.  This will happen if you have relocated the documents folder to a network location or to the cloud.


    \_(ツ)_/

    yep, thats why im unblocking the files. the loading of assemblies and everything works just fine when i run this script in a new powershell session. i dont get any errors. what im trying to do is lets say 5 new assemblies are copied in the DLL folder in the future by someone from the internet. the files will be blocked and this is what the error will display to future developer who copies these 5 new assemblies: 

    Operation is not supported. (Exception from HRESULT: 0x80131515) )

    so in order to make it easier right away for the developer to understand what COULD be the issue, i want to display this error with explanation, which is what the "parent" try catch is doing here ""

    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

    thats just my style of scripting. i like outputting errors in a meaningful way, THEN reoslving it programmtically, then there would be a 2nd try catch block in case a new error pops up besides that error. 

    Thursday, January 23, 2020 8:10 PM
  • None of that has anything to do with your error.  You must unblock all files every time before trying to load any.  If the folder is on the network then unblocking won't help if the network location is not trusted.

    "Networking with Windows 101"


    \_(ツ)_/

    Thursday, January 23, 2020 8:14 PM
    Moderator
  • None of that has anything to do with your error.  You must unblock all files every time before trying to load any.  If the folder is on the network then unblocking won't help if the network location is not trusted.

    "Networking with Windows 101"


    \_(ツ)_/

    i think i found a way. ill have to nest another try catch after unblocking the file to load it. it seems like if i unblock the file AND try loading it again within one try, then the loading is not recognizing the unblocking of the file. its like it needs a new instance (new session, new try catch). currently testing...
    Saturday, January 25, 2020 6:21 PM
  • That makes absolutely no sense.  The change in code accomplishes nothing.  A Try/Catch is not a session it just pushes and exception pointer onto the stack and jumps to it when the system detects an error.  The code still executes in exactly the same way.

    You have to unblock all files before trying to load any file.  The dependencies will cause an error if they are not unblocked before the load.

    If the location you are loading from is untrusted then unblocking can never work.  Only trusted locations will allow a file to be unblocked.


    \_(ツ)_/

    Saturday, January 25, 2020 6:45 PM
    Moderator
  • That makes absolutely no sense.  The change in code accomplishes nothing.  A Try/Catch is not a session it just pushes and exception pointer onto the stack and jumps to it when the system detects an error.  The code still executes in exactly the same way.

    You have to unblock all files before trying to load any file.  The dependencies will cause an error if they are not unblocked before the load.

    If the location you are loading from is untrusted then unblocking can never work.  Only trusted locations will allow a file to be unblocked.


    \_(ツ)_/

    the location im loading from is trusted. but the reason the files were blocked is because i copied them from a 3rd part software, into this trusted location, which then i would understand why they are blocked based on my research online when i looked into the error "operation is not supported". 

    so ya the files are blocked in the trusted location now and need unblocking before they can be loaded. i could easily unblock them but i want to script error catching first in case a developer in the future one day adds more assembly files like this from an outside source like i did into this trusted location. instead of him having to research the error then "operation is not supported", i want the script to display the error, explain what it is like i ave with my write host statements, then unblock the files and try loading the assembly file afterwards. 

    Saturday, January 25, 2020 7:51 PM
  • You are trying to reinvent the wheel.  Just enumerate all files and unblock them.  Forget about the exception part as it will not help you.

    YOU are trying to solve a problem that does not exist.  YOu can unblock any file even if it is not blocked and trying to unblock a file in an exception handler will cause the file to be unavailable as it will still be open.  This is because the exception handler is called in the system context before the open file handle has been released in the PowerShell instance.


    \_(ツ)_/

    Saturday, January 25, 2020 10:09 PM
    Moderator
  • You are trying to reinvent the wheel.  Just enumerate all files and unblock them.  Forget about the exception part as it will not help you.

    YOU are trying to solve a problem that does not exist.  YOu can unblock any file even if it is not blocked and trying to unblock a file in an exception handler will cause the file to be unavailable as it will still be open.  This is because the exception handler is called in the system context before the open file handle has been released in the PowerShell instance.


    \_(ツ)_/

    ok, ill probably just forget it and enumerate the files as you recommended. better yet ill just do
    Get-Childitem ./*.* -Filter .dll | unblock-file 

    Saturday, January 25, 2020 10:46 PM
  • Here's a way to search for blocked files.  You could pipe this to remove-item to unblock them. 

    get-item * -stream zone*

    Unblock the file:

    get-item * -stream zone* | Remove-Item

    Recursive search:

    get-childitem -recurse | get-item -stream zone*

    • Edited by JS2010 Sunday, January 26, 2020 7:55 AM
    Saturday, January 25, 2020 11:17 PM
  • Here's a way to search for blocked files.  You could pipe this to remove-item to unblock them. 


    get-item * -stream zone*

    If you are goinf to do it you will have to code it correctly.  Your code will not get anything.

     Get-Childitem ./*.dll | Unblock-File -Verbose


    \_(ツ)_/


    • Edited by jrvModerator Saturday, January 25, 2020 11:59 PM
    • Marked as answer by cataster Tuesday, January 28, 2020 1:38 PM
    Saturday, January 25, 2020 11:58 PM
    Moderator