在 Powershell 中模拟使用 WindowsIdentity 抛出 FileNotFoundException

发布于 2024-11-25 16:16:39 字数 2217 浏览 3 评论 0原文

我在 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());

其中 CmdletMap[PSVocab.OsBootTime] 的 PS 脚本很简单:

$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

并且调试显示它在 RunspaceConfiguration.Create() 处失败。但不知道为什么。

该 DLL 已在 GAC 中注册,并在项目本身中被引用。还确认路径和版本正确。

参考资料取自:

有人能对此有深入的了解吗?

I am encountering a somewhat weird error with performing impersonation in PowerShell and C#. Executing the folowing code does not show any errors.

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());

where the PS script for CmdletMap[PSVocab.OsBootTime] is simply:

$info = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
        ; $info.ConvertToDateTime($info.LastBootUpTime)

The above C# code works fine locally. However, once I had this same block with Windows impersonation like so:

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 } 

I get the following exception:

FileNotFoundException: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

and debugging shows that it fails at RunspaceConfiguration.Create(). Not sure why though.

The DLL though is already registered in the GAC though as well as being referenced in the project itself. Also confirmed that the paths and version are correct.

References taken from:

Can someone give an insight to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

决绝 2024-12-02 16:16:39

您模拟的用户可能没有足够的权限来访问 GAC 中必要的 powershell 文件。

作为快速尝试,尝试授予用户(您正在模拟)本地管理员权限,看看它是否有效。如果可行,请撤销本地管理员权限并根据需要添加文件权限。

The user you are impersonating into may not have enough permissions to access the necessary powershell files in the GAC.

As a quick shot try to give the user (you are impersonating into) local admin rights to see if it works then. It this works, revoke local admin rights and add file permissions as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文