这是我第一次尝试从C#应用程序执行PowerShell脚本.我正在使用PowerShell,因为我需要从远程计算机上执行的.exe输出.我能够使用WMI在远程计算机上运行.exe,但是我无法获得所需的输出.
无论如何,我在过去的一天左右就已经开始这样了,我已经浏览了网络,并在SO处寻找类似的问题,但似乎无法弄清楚问题.我正在尝试从远程计算机上的.NET 4.0应用程序运行一个简单的PowerShell命令.当我以管理员身份运行Visual Studio 2013时,以下代码执行正常:
PowerShell ps = PowerShell.Create();
ps.AddScript(@"Invoke-Command {c:\path\to\file.exe /p} -computername <computerName>");
results = ps.Invoke();
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果.但是,当我作为非管理员运行VS时,代码似乎执行正常(没有例外),但我没有得到任何结果.环顾四周后,我添加了如下假冒:
using (var impersonator = new Impersonator("username", "domain", "password"))
{
PowerShell ps = PowerShell.Create();
ps.AddScript(@"Invoke-Command {c:\path\to\file.exe /p} -computername <computerName>");
results = ps.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
但是,ps.Invoke方法开始抛出System.Security.SecurityException - "不允许请求的注册表访问".这是堆栈跟踪:
在System.Management的System.Management.Automation.ModuleIntrinsics.GetExpandedEnvironmentVariable(String name,EnvironmentVariableTarget target)的System.Environment.GetEnvironmentVariable(String variable,EnvironmentVariableTarget target)上的Microsoft.Win32.RegistryKey.OpenSubKey(String name,Boolean writable). System.Management.Automation.ExecutionContext..ctor上System.Management.Automation.ExecutionContext.InitializeCommon(AutomationEngine engine,PSHost hostInterface)的System.Management.Automation.ModuleIntrinsics..ctor(ExecutionContext context)中的Automation.ModuleIntrinsics.SetModulePath() System.Management.Automation.Automation.Runspaces.LocalRunspace.DoOpenHelper()在System.Management上的System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface,RunspaceConfiguration runspaceConfiguration,InitialSessionState iss)中的(AutomationEngine引擎,PSHost hostInterface,RunspaceConfiguration runspaceConfiguration). Automation.Runspaces.LocalRunspace.OpenHelper(布尔同步 所有)System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall)at System.Management.Automation.Runspaces.RunspaceBase.Open()at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse,Boolean isSync) )System.Management.Automation.PowerShell.CoreInvokeHelper [TInput,TOutput](PSDataCollection
1 input, PSDataCollection1输出,PSInvocationSettings设置)在System.Management.Automation.PowerShell.CoreInvoke [TInput,TOutput](PSDataCollection1 input, PSDataCollection1输出,PSInvocationSettings设置)中. System.Management.Automation.PowerShell.Invoke上的System.Management.Automation.PowerShell.Invoke(IEnumerable输入,PSInvocationSettings设置)中的Management.Automation.PowerShell.CoreInvoke [TOutput](IEnumerable输入,PSDataCollection`1输出,PSInvocationSettings设置)( …
我需要从C#执行几个powershell命令,而我正在使用此代码
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddCommand("Add-PSSnapin").AddArgument("Citrix*");
ps.Invoke();
// other commands ...
Run Code Online (Sandbox Code Playgroud)
这可以正常工作,但是现在没有足够权限使用Powershell的用户应该执行此应用程序。有没有一种方法可以使用不同的凭据执行Powershell代码?我的意思是这样的
var password = new SecureString();
Array.ForEach("myStup1dPa$$w0rd".ToCharArray(), password.AppendChar);
PSCredential credential = new PSCredential("serviceUser", password);
// here I miss the way to link this credential object to ps Powershell object...
Run Code Online (Sandbox Code Playgroud)