如何从C#运行PowerShell脚本
我正在尝试使用C#运行PowerShell脚本,但我没有任何成功。 这是我的function:
private void ExecutePowerShellCommand(string scriptfile) { RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(); scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); Pipeline pipeline = runspace.CreatePipeline(); //Here's how you add a new script with arguments Command myCommand = new Command(scriptfile); //CommandParameter testParam = new CommandParameter("key", "value"); //myCommand.Parameters.Add(testParam); pipeline.Commands.Add(myCommand); // Execute PowerShell script pipeline.Invoke(); }
这是我得到的错误:
访问注册表项“HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ PowerShell \ 1 \ ShellIds \ Microsoft.PowerShell”被拒绝。
我该如何解决这个问题? 我已经看到了冒充的想法,但我似乎没有找到任何好的榜样来冒充。 我想以管理员身份运行此脚本。
我做了以下声明:
[DllImport("advapi32.dll")] private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("kernel32.dll")] private static extern bool CloseHandle(IntPtr handle); public delegate void IncognitoDelegate(params object[] args);
我为模拟创建了以下函数:
public static void Impersonate(IncognitoDelegate incognitoDelegate, params object[] args) { System.IntPtr token = new IntPtr(); WindowsIdentity wi; if (LogonUser("myusername", "", "mypassword", 8, 0, ref token)) { wi = new WindowsIdentity(token); WindowsImpersonationContext wic = wi.Impersonate(); incognitoDelegate(args); wic.Undo(); } CloseHandle(token); }
我创建了一个用作委托的函数:
private static void GIncognito(params object[] args) { RunspaceInvoke scriptInvoker = new RunspaceInvoke(); scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); }
我修改了我的方法:
private void ExecutePowerShellCommand(string scriptfile) { RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); Impersonate(new Util.IncognitoDelegate(GIncognito)); //RunspaceInvoke scriptInvoker = new RunspaceInvoke(); //scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); Pipeline pipeline = runspace.CreatePipeline(); //Here's how you add a new script with arguments Command myCommand = new Command(scriptfile); //CommandParameter testParam = new CommandParameter("key", "value"); //myCommand.Parameters.Add(testParam); pipeline.Commands.Add(myCommand); // Execute PowerShell script pipeline.Invoke(); }
结果是……
…非常山姆的错误,告诉我我无法访问注册表项。
默认的Set-ExecutionPolicy
命令尝试设置机器范围的值。 您只想更改C#应用程序范围内的设置,因此您应该在命令中添加-Scope Process
选项。
使用Get-Help Set-ExecutionPolicy -detailed
显示以下信息:
注意:要更改默认(LocalMachine)范围的执行策略,请使用“以管理员身份运行”选项启动Windows PowerShell。
…它还描述了-Scope
选项。
这样做的好处是只影响从C#应用程序运行的脚本的执行策略,并且不会不必要地更改默认PowerShell行为的执行策略。 (因此它更安全,特别是如果您可以保证应用程序运行的脚本的有效性。)
这是方法b,不需要提升权限或注册表修改权限 。
使用Process.Start
启动它并添加相关的initial -command
或-file
args。
%SystemRoot%\ system32 \ WindowsPowerShell \ v1.0 \ powershell.exe -ExecutionPolicy bypass
这是另一种技术, http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/
这依赖于首先对其进行编码并通过powershell.exe的-EncodedCommand
arg传递int,这似乎绕过了执行策略。
您可以尝试以下内容
using ( new Impersonator( "Username", "DomainName", "Password" ) ) { using (RunspaceInvoke invoker = new RunspaceInvoke()) { invoker.Invoke("Set-ExecutionPolicy Unrestricted"); } }
这是一个链接,你可以看一下想法作为一个例子, 模拟用户模拟 。