Jae*_*Jae 6 c# powershell impersonation windows-identity
我在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());
Run Code Online (Sandbox Code Playgroud)
PS脚本的CmdletMap[PSVocab.OsBootTime]
简单地说是:
$info = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
; $info.ConvertToDateTime($info.LastBootUpTime)
Run Code Online (Sandbox Code Playgroud)
上面的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 }
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
FileNotFoundException: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
Run Code Online (Sandbox Code Playgroud)
并且调试显示它失败了RunspaceConfiguration.Create()
.不知道为什么.
虽然DLL已经在GAC中注册,但在项目本身中被引用.还确认路径和版本是正确的.
参考文献来自:
有人可以对此有所了解吗?
您模拟的用户可能没有足够的权限来访问 GAC 中必要的 powershell 文件。
作为快速尝试,尝试授予用户(您正在模拟)本地管理员权限,看看它是否有效。如果可行,请撤销本地管理员权限并根据需要添加文件权限。
归档时间: |
|
查看次数: |
1009 次 |
最近记录: |