Add-Type load assembly from network UNC share error 0x80131515
Asked Answered
C

3

6

When you want to add an assembly from a network UNC share using the command:

$scriptPath = Split-Path ($MyInvocation.MyCommand.Path)
Add-Type -path "$scriptPath\selenium-dotnet\net40\WebDriver.dll"

you might face such an error:

Add-Type: Could not load file or assembly 'file:///Z:\A-Backup\Users\Administr
ator\Desktop\MAXIMO Automatic\selenium-dotnet\net40\WebDriver.dll' or one of it
s dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515
)
At Z:\A-Backup\Users\Administrator\Desktop\MAXIMO Automatic\MAXIMO Automatic.ps
1:14 char:1
+ Add-Type -path "$scriptPath\selenium-dotnet\net40\WebDriver.dll"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell
   .Commands.AddTypeCommand

How can I fix this problem?

Coussoule answered 13/11, 2013 at 15:4 Comment(0)
C
10

The key is to allow for loading an assembly from a network path for a PowerShell executable. It can be done by creating the two files

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

and paste this code:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>
Coussoule answered 13/11, 2013 at 15:5 Comment(2)
It seems you can not do this in Windows Server 2012 R2 - access denied.Harrietteharrigan
Login in as a local administrator and change the ownership of the file to your account. Then assign your account modification access to the files, make the change and revert the security changesAlliance
C
4

Re: ALIENQuake's. I've added your fix to a PS script to install the files in the correct locations.

$PSPaths = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config','C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe.config'
$XMLCode = @"
<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
   <runtime>
         <loadFromRemoteSources enabled="true"/>
    </runtime>
</configuration>
"@
foreach($PSConfigFile in $PSPaths) {
    $xmlcode | Out-File -FilePath $PSConfigFile -Encoding utf8 
}
Camilia answered 16/1, 2015 at 19:51 Comment(0)
A
4

Instead of Add-Type you could:

[System.Reflection.Assembly]::UnsafeLoadFrom('\\uncpath\driver.dll')

It'll ignore some security settings and load the library but it's, well, unsafe ;).

Ashy answered 30/6, 2017 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.