Я столкнулся с несколько странной ошибкой при выполнении олицетворения в PowerShell и C #. Выполнение следующего кода не показывает никаких ошибок.
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());
где сценарий PS для CmdletMap [PSVocab.OsBootTime ]
просто:
$info = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
; $info.ConvertToDateTime($info.LastBootUpTime)
Приведенный выше код C # отлично работает локально.Однако, как только у меня был такой же блок с олицетворением Windows, например:
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 }
, я получаю следующее исключение:
FileNotFoundException: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
и отладка показывает, что он не работает at RunspaceConfiguration.Create ()
. Не уверен, почему.
Хотя DLL уже зарегистрирована в GAC, а также упоминается в самом проекте. Также подтверждено, что пути и версия верны .
Ссылки взяты из:
Может ли кто-нибудь рассказать об этом?