从 C# 以管理员身份执行 PowerShell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1313673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:00:26  来源:igfitidea点击:

Execute PowerShell as an administrator from C#

c#powershell

提问by Emmett

I have the following C# code

我有以下 C# 代码

using (RunspaceInvoke invoker = new RunspaceInvoke())
{
  invoker.Invoke("Set-ExecutionPolicy Unrestricted");
  // ...
}

which gives me the exception

这给了我例外

Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell”的访问被拒绝。

According to this, the solution is to start PowerShell as an administrator.

根据,解决的办法是启动PowerShell中以管理员身份。

Ordinarily, this can be accomplished by right-clicking PowerShell and selecting "Run as Administrator". Is there a way to do this programmatically?

通常,这可以通过右键单击 PowerShell 并选择“以管理员身份运行”来完成。有没有办法以编程方式做到这一点?

采纳答案by juan

Check thisout

检查

You need to impersonate as an administrator to do it (you will of course need administrator credentials)

您需要冒充管理员来执行此操作(您当然需要管理员凭据)

Check that article, that also comes with code ready to use (I've used it and it works great)

检查那篇文章,它还附带了可以使用的代码(我已经使用过它并且效果很好)

Basically, you need to do this:

基本上,你需要这样做:

using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
    using (RunspaceInvoke invoker = new RunspaceInvoke())
    {
        invoker.Invoke("Set-ExecutionPolicy Unrestricted");
    }
}

回答by x0n

Administrative privileges are at the applicationlevel. The app that needs admin access in this case is yours. Creating runspaces in C# in a custom app does not invoke powershell the application - it just loads some assemblies into your application.

管理权限位于应用程序级别。在这种情况下需要管理员访问权限的应用程序是您的。在自定义应用程序中使用 C# 创建运行空间不会调用应用程序的 powershell - 它只是将一些程序集加载到您的应用程序中。

That said, you can elevate as the other poster said although embedding admin usernames and passwords into source code make me feel ill.

也就是说,尽管将管理员用户名和密码嵌入到源代码中让我感觉不舒服,但您可以像其他海报所说的那样提升。

-Oisin

-Oisin

回答by AndyM

I think an alternative model would be to wrap the powershell executor into a simple asp.net webapi webservice.

我认为另一种模型是将 powershell 执行器包装成一个简单的 asp.net webapi webservice。

The webservice could then be configured to run with the required permissions needed to do it's job. It can provide it's own security to determine which clients can call it.

然后可以将 Web 服务配置为以完成其工作所需的权限运行。它可以提供自己的安全性来确定哪些客户端可以调用它。

To execute a script, you would just call webservice methods. You could make the method quite general - script name and params.

要执行脚本,您只需调用 webservice 方法。您可以使该方法非常通用 - 脚本名称和参数。

It's a bit more work, but a lot more secure (see x0n's thoughts).

这需要更多的工作,但更安全(请参阅 x0n 的想法)。

回答by BradleyMorgan

I know this is an old post, but we ran into this same problem recently.

我知道这是一个旧帖子,但我们最近遇到了同样的问题。

We had to scope the execution policy on the machine running the C# code by running the following from PowerShell...

我们必须通过从 PowerShell 运行以下命令来确定运行 C# 代码的机器上的执行策略...

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

When we had done this previously, without scoping, we were setting the execution policy for Administrator. Visual Studio \ C# was running as the current user, causing it to fail with insufficient permissions.

当我们之前完成此操作时,在没有范围界定的情况下,我们正在为管理员设置执行策略。Visual Studio \ C# 以当前用户身份运行,导致它因权限不足而失败。

回答by Omkar Telee

Strictly for DEV environmentThis is relatively very old post. But I have found a new way to do this. I am hosting the C# web api on IIS 8 having some powershell code that I want to run with administrator privileges.

严格针对 DEV 环境这是比较老的帖子。但是我找到了一种新的方法来做到这一点。我在 IIS 8 上托管 C# web api,有一些我想以管理员权限运行的 powershell 代码。

So I provided the admin credentials in the Application pool identity setting.

所以我在应用程序池身份设置中提供了管理员凭据。

IIS8 on Windows 2012 server

Windows 2012 服务器上的 IIS8

Just set administrator account in app pool identity.

只需在应用程序池标识中设置管理员帐户。

Hope this helps to anyone. :)

希望这对任何人都有帮助。:)