Powershell load dll got error: Add-Type : Could not load file or assembly 'WebDriver.dll' or one of its dependencies. Operation is not supported
Asked Answered
N

4

26

I want to use PowerShell with selenium and download selenium from http://www.java2s.com/Code/Jar/s/Downloadseleniumremotedriver2350jar.htm . When I try to load one of the dlls, I got errors. Hope someone can help me with it.

This is my system info.

OS Name:                   Microsoft Windows 7 Enterprise
OS Version:                6.1.7601 Service Pack 1 Build 7601
OS Manufacturer:           Microsoft Corporation

This is my PowerShell info.

PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> $psversiontable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.18052
BuildVersion                   6.3.9421.0
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2


PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40>

This is the errors I got when trying to load the dll.

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> Add-Type -Path .\WebDriver.dll
    Add-Type : Could not load file or assembly 'file:///C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll' or one of its dependencies. Operation is
    not supported. (Exception from HRESULT: 0x80131515)
    At line:1 char:1
    + Add-Type -Path .\WebDriver.dll
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
        + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom(".\WebDriver.dll")
    Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\WebDriver.dll' or one of its dependencies. The system
    cannot find the file specified."
    At line:1 char:1
    + [reflection.assembly]::LoadFrom(".\WebDriver.dll")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FileNotFoundException

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom("WebDriver.dll")
    Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\WebDriver.dll' or one of its dependencies. The system
    cannot find the file specified."
    At line:1 char:1
    + [reflection.assembly]::LoadFrom("WebDriver.dll")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FileNotFoundException

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom("C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll")
    Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll' or
    one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
    At line:1 char:1
    + [reflection.assembly]::LoadFrom("C:\Users\test\Downloads\selenium-dotnet-2.35.0 ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FileLoadException

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFile("C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll")
    Exception calling "LoadFile" with "1" argument(s): "An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed

 in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If
    this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more
    information."
    At line:1 char:1
    + [reflection.assembly]::LoadFile("C:\Users\test\Downloads\selenium-dotnet-2.35.0 ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : NotSupportedException

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40>
Nudism answered 14/9, 2013 at 11:49 Comment(0)
P
55

If you downloaded the .DLLs from the internet, Powershell by default will not trust them. You can do one of two things here:

  1. Unblock the content. Here's a guide with some elaboration on the problem, but essentially you just need to right-click on the downloaded file, select Properties, and click 'Unblock' in the general tab. You can unblock content directly from powershell by using the Unblock-File cmdlet.
  2. Change your execution policy. This can also allow malicious scripts that you download to run, so be cautious.

Specifically reviewing and unblocking content you trust seems the better route, especially for your case since you seem like you would only be doing this once and you trust the package.


Edit: If that didn't solve that, the other experience I've had with this type of error is when Powershell didn't the same version of the .NET runtime as the assembly. See this question for some context, where Powershell 2.0 ran the .NET 2 runtime and the asker needed .NET 4 for some assemblies.

You're not using the same version of PS so your mileage may vary, but I would try creating/editing a config file per the linked answer to support the runtime of the DLLs you are using.

Pavla answered 14/9, 2013 at 18:31 Comment(5)
@Yousui Including the Operation is not supported. (Exception from HRESULT: 0x80131515) part in the first error?Pavla
If you are working in the powershell console it's necessary to reopen it after unblocking the files.Duodenum
For others, if the above doesn't resolve the issue, check that you aren't trying to load x86 DLLs into a 64-bit PowerShell instance. Simply using PowerShell (x86) can solve the problem if you are!Insider
Unblock-File -Path $myDllPath worked like a charm.Teasel
@Insider You're my hero. Looking for two hours, why my script is unstable (sometimes it works flawless, sometimes I get no data, sometimes it fails including a DLL). Running the x86 version of ISE fixes all issues. Thanks.Moonlight
S
7

This worked for me: (from https://mcmap.net/q/535907/-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>
Sudra answered 20/1, 2014 at 22:39 Comment(4)
powershell.exe.config doesn't exist in current versions of powershell, and powershell_ise.exe.config is not editable - permissions are locked down even for administrators so that the file cannot be written to.Hasheem
That's simply not true. This solution worked for me running PowerShell 4.0. The difference was that I had to take control of the folder from SYSTEM and edit .\powershell_ise.exe.config. Then I put the permissions back the way they were and voila! It worked. This is a good answer.Unfledged
In my case, powershell was working fine but powershell_ise was exhibiting issues with Add-Type. Just adding the file C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config (formerly didn't exist) by copying from another machine solved it for me. I didn't even need to specify a <runtime> setting.Tenpins
The assumption here is that the DLLs are in a remote location. Moving them to a local location will also avoid the error. What is local or remote is probably determined by Security Zones.Choleric
D
1

Run as Administrator has solved the issue, although my get-executionpolicy is allsigned

Decury answered 23/10, 2019 at 13:41 Comment(3)
What does this answer add that those of 6 years ago don't cover, apart from running security risks introduced by running as an admin, unnecessarily?Anallese
@Anallese It appears to give another solution to a problem that might be useful for someone, anyone that is running anything as admin should know what he is getting himself into.Nickerson
@Anallese - I'd just spent half an hour with our support team talking them through unblocking files and changing execution contexts etc. Turned out that this was the only thing we needed to do. So it's definitely a helpful solution to the problem. (+1)Tengler
W
0

What I've done was

  1. Right click click the file, click properties the check Unlock the .dll
  2. Close PS and reOpen again
  3. run the .ps1 script
Wandering answered 12/5, 2023 at 3:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.