在C#代码中使用PSCredentials

时间:2016-09-23 00:13:15

标签: c# powershell impersonation

我正在编写二进制cmdlet,我想接受PSCredential参数以使用提升的凭据。

[Parameter]
public PSCredential Credential { get; set; }

似乎缺少明显的文档 - 我只能找到如何从C#执行PowerShell cmdlet,这不是我想要做的。

我下载了SimpleImpersonation nuget包,以避免自己进行Windows模拟。

using (Impersonation.LogonUser("GLOBAL", "last.first", "mypassword", LogonType.Interactive))
{
    foreach (ServerInfo server in Targets)
        ProcessRecord();
}

当我检查System.Security.Principal.WindowsIdentity.GetCurrent().Name用户是否正确时,但当我执行命令(例如ServerManager.OpenRemote(computerName))时,我收到UnauthorizedAccessException。如果我从作为所需用户运行的PowerShell实例运行此代码,则cmdlet将执行完美。

无论如何都有关于如何在C#二进制cmdlet中使用PSCredential的任何线索?

1 个答案:

答案 0 :(得分:1)

我不确定为什么在网上有这么少的文档。我怀疑我需要使用模仿来实现这一目标。似乎有很多涉及这个问题,see here

在进行了一些调查之后,有一个名为SimpleImpersonation的好的nuget包。

使用:

[Parameter]
public PSCredential Credential { get; set; }

我可以这样做:

using (Impersonation.LogonUser("GLOBAL", Credential.UserName, Credential.Password, LogonType.Interactive))

这允许我以模拟用户身份运行命令。 LogonUser()使用用户名和SecureString密码。

这适用于ServiceController次调用,但ServerManager仍然会引发COM未经授权的异常。我设法找到this post,表示不使用OpenRemote()但是使用ctor重载。以下代码有效:

using (ServerManager serverManager = new ServerManager(@"\\server\c$\windows\system32\inetsrv\config\applicationHost.config"))
{
    ApplicationPoolCollection appPool = serverManager.ApplicationPools;
    Console.WriteLine(appPool.Count);
}