[System.Reflection.Assembly] :: Load(byte [])失败:系统找不到指定的文件

时间:2019-06-27 21:07:57

标签: c# powershell

我有一个用C#编写的Windows服务,该服务执行Powershell命令。它尝试加载Azure存储dll并失败。

$bytes = [System.IO.File]::ReadAllBytes($azureStorageDll) // local path of the dll
[System.Reflection.Assembly]::Load($bytes) | Out-Null

该dll位于软件包版本9.0.0中。错误是Exception calling "Load" with "1" argument(s): "Could not load file or assembly 'Microsoft.WindowsAzure.Storage, Version=9.3.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

有趣的是,如果我使用自己的凭据运行Windows服务,则它可以工作。但是在bot帐户下运行时失败。而且9.3.2甚至不是最新版本

我尝试使用bot帐户打印出GAC,但没有看到与Azure存储相关的任何信息

$n1 = New-PSDrive -Name HKCR -PSProvider 'Microsoft.PowerShell.Core\Registry' -Root HKEY_CLASSES_ROOT
$n2 = Get-ItemProperty -Path 'HKCR:\Installer\Assemblies\Global' | Get-Member -MemberType NoteProperty

我了解加载dll时会遇到各种情况。但是我不知道该如何调试?有人可以帮忙一些建议吗?

1 个答案:

答案 0 :(得分:1)

尝试启用Fusion日志记录,然后以bot用户身份登录Windows并尝试加载DLL。融合日志可能会显示,由于文件系统权限,您的服务帐户无法读取一个依赖的DLL。

这里有一个Powershell script,可以更轻松地切换Fusion日志记录的状态:

Param( $LogPath = "c:\temp\Fusion" )

#restart as admin if not started as admin 
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { 
    Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -PassThru -Verb RunAs | Out-null; exit 
}
$RegKey = 'HKLM:\SOFTWARE\Microsoft\Fusion'
If(!(Test-Path $RegKey)) {New-Item -Path $RegKey -ItemType Container | Out-Null}
$NewValue = If((Get-ItemProperty $RegKey).EnableLog -eq 1){0} Else {1}
Set-ItemProperty -Path $RegKey -Name EnableLog        -Value $NewValue -Type DWord
Set-ItemProperty -Path $RegKey -Name ForceLog         -Value $NewValue -Type DWord
Set-ItemProperty -Path $RegKey -Name LogFailures      -Value $NewValue -Type DWord
Set-ItemProperty -Path $RegKey -Name LogResourceBinds -Value $NewValue -Type DWord
Set-ItemProperty -Path $RegKey -Name LogPath          -Value $LogPath  -Type String
If(!(Test-Path $LogPath -PathType Container)){New-Item $LogPath -ItemType Directory | Out-Null}
Write-Host "$(If($NewValue -eq 1){'Enabled'}Else{'Disabled'}) Fusion Logging at $LogPath"
If(!$Host.Name.Contains("ISE")){Pause}
相关问题