模拟在Powershell中使用WindowsIdentity抛出FileNotFoundException [英] Impersonation throws FileNotFoundException with WindowsIdentity in Powershell

查看:174
本文介绍了模拟在Powershell中使用WindowsIdentity抛出FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PowerShell和C#中执行模拟时遇到一个有点奇怪的错误.执行以下代码不会显示任何错误.

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

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

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

以上C#代码在本地运行良好.但是,一旦我对Windows模拟使用了相同的代码块,如下所示:

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 } 

我收到以下异常:

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

并且调试显示它在RunspaceConfiguration.Create()失败.不确定为什么.

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

尽管DLL已在GAC中注册,但已在项目本身中被引用.还确认路径和版本正确.

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.

引用来自:

  • Impersonation and Hosting PowerShell
  • ASP.NET PowerShell Impersonation

有人可以对此提供见解吗?

Can someone give an insight to this?

推荐答案

您要模拟的用户可能没有足够的权限来访问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.

这篇关于模拟在Powershell中使用WindowsIdentity抛出FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆