PowerShell runtime exception - “could not load file or assembly”

后端 未结 5 1587
既然无缘
既然无缘 2021-01-11 11:27

This seems to be a common problem in PowerShell and Visual Studio, yet cases and solutions seem to vary a lot. Though seeing several similar questions, I didn\'t find a work

相关标签:
5条回答
  • 2021-01-11 11:42
    Import-Module : Could not load file or assembly 'file:/// *dll path*' or one of 
    its dependencies. An attempt was made to load a program with an incorrect format.
    At *script path*.ps1:68 char:2
    +     Import-Module *module path*
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Import-Module], BadImageFormatException
        + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.ImportModuleCommand
    

    If you see the above error, it might be the issue of running the Windows PowerShell ISE in 32 bit mode(Windows PowerShell ISE(x86)). You have to run the application Windows PowerShell ISE.

    This application(Windows PowerShell ISE) is in 64 bit mode. When I executed the power shell command, its working fine for me.

    I have been trying to fix this with different types solutions but it doesn't worked for me. If you running it 32 mode, switch it to 64 bit and try.

    0 讨论(0)
  • 2021-01-11 11:42

    I would look at WinSCP.dll dependencies in ILDASM or dotPeek and then load those dependencies before loading WinSCP.dll. The problem is that you are running in PowerShell.exe (or powershell_ise.exe) fusion load context and the assemblies that WinSCP.dll aren't going to be found under these two exe's base dirs. So you will need to load the dependent assemblies before the CLR loader chokes because it can't find a required DLL. If you need help figuring out which assembly can't be found, check out the fuslogvw.exe tool.

    BTW, as of PowerShell v2 you should use Add-Type -Path <path> instead of [S.R.A]::LoadFrom(...).

    0 讨论(0)
  • 2021-01-11 11:51

    The following worked for me (from Stack Overflow question Add-Type load assembly from network UNC share error 0x80131515).

    In files:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <runtime>
          <loadFromRemoteSources enabled="true"/>
       </runtime>
    </configuration>
    
    0 讨论(0)
  • 2021-01-11 12:05

    The file may be blocked from being loaded because of its origin (stored in NTFS streams). Check properties and see if the little unblock button is lit up. Once you unblock it perhaps it will load...

    0 讨论(0)
  • 2021-01-11 12:05

    Solution in this case: Remove and download it all again. I even unblocked it on the first file, with no help. Glad this was on an internal machine.

    0 讨论(0)
提交回复
热议问题