从网络UNC共享错误0x80131515添加类型加载程序集

时间:2013-11-13 15:04:55

标签: powershell selenium add-type

如果要使用以下命令从网络UNC共享添加程序集:

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

你可能会遇到这样的错误:

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

如何解决此问题?

3 个答案:

答案 0 :(得分:10)

关键是允许从网络路径加载PowerShell可执行文件来加载程序集。可以通过创建两个文件

来完成

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>

答案 1 :(得分:4)

回复:ALIENQuake的。我已将您的修复程序添加到PS脚本中,以便将文件安装在正确的位置。

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

答案 2 :(得分:3)

而不是Add-Type你可以:

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

它会忽略一些安全设置并加载库,但它确实不安全;)。