PowerShellとC#で偽装を実行する際に幾分奇妙なエラーが発生します。フォローコードを実行してもエラーは表示されません。上記のC#コードは、ローカルで正常に動作しますPowershellでWindowsIdentityで偽装するとFileNotFoundExceptionがスローされる
$info = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
; $info.ConvertToDateTime($info.LastBootUpTime)
:CmdletMap[PSVocab.OsBootTime]
ためのPSスクリプトは単純である
PSObject result = null;
using (PowerShell powershell = PowerShell.Create())
{
RunspaceConfiguration config = RunspaceConfiguration.Create();
powershell.Runspace = RunspaceFactory.CreateRunspace(config);
powershell.Runspace.Open();
powershell.AddScript(String.Format(CmdletMap[PSVocab.OsBootTime],
this.ComputerName));
result = powershell.Invoke().First();
powershell.Runspace.Close();
}
return DateTime.Parse(result.ToString());
。
FileNotFoundException: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
とデバッグが、それはRunspaceConfiguration.Create()
で失敗したことを示しています
WindowsIdentity ImpersonatedIdentity = new WindowsIdentity(ImpersonateUserName);
WindowsImpersonationContext impersonatedContext
= ImpersonatedIdentity.Impersonate();
try
{
PSObject result = null;
using (PowerShell powershell = PowerShell.Create())
{
RunspaceConfiguration config = RunspaceConfiguration.Create();
powershell.Runspace = RunspaceFactory.CreateRunspace(config);
powershell.Runspace.Open();
powershell.AddScript(String.Format(CmdletMap[PSVocab.OsBootTime],
this.ComputerName));
result = powershell.Invoke().First();
powershell.Runspace.Close();
}
return DateTime.Parse(result.ToString());
} catch (Exception ex) { // do logging here }
が、私は次の例外を取得:しかし、かつて私はそうのように、Windowsの偽装と同じブロックを持っていました。しかし、なぜか分からない。
しかし、DLLは既にGACに登録されていますが、プロジェクト自体で参照されています。また、パスとバージョンが正しいことを確認しました。取ら
参考文献:
は、誰かがこれに洞察力を与えることができますか?