[英]Invoke remote powershell command from C#
I'm trying to run an invoke-command cmdlet using C# but I can't figure out the right syntax. 我正在尝试使用C#运行invoke-command cmdlet,但我无法找出正确的语法。 I just want to run this simple command:
我只想运行这个简单的命令:
invoke-command -ComputerName mycomp.mylab.com -ScriptBlock {"get-childitem C:\windows"}
In C# code, I have done the following: 在C#代码中,我完成了以下操作:
InitialSessionState initial = InitialSessionState.CreateDefault();
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddCommand("invoke-command");
ps.AddParameter("ComputerName", "mycomp.mylab.com");
ps.AddParameter("ScriptBlock", "get-childitem C:\\windows");
foreach (PSObject obj in ps.Invoke())
{
// Do Something
}
When I run this, I get an exception: 当我运行它时,我得到一个例外:
Cannot bind parameter 'ScriptBlock'. Cannot convert the "get-childitem C:\windows" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
I'm guessing I need to use ScriptBlock type here somewhere, but don't know how to. 我猜我需要在某处使用ScriptBlock类型,但不知道该怎么做。 This is just a simple example to get started with, the real use case would involve running a larger script block with multiple commands in it, so any help on how to do this would be highly appreciated.
这只是一个简单的示例,真正的用例将涉及运行一个包含多个命令的更大的脚本块,因此任何有关如何执行此操作的帮助都将受到高度赞赏。
Thanks 谢谢
Ah, the parameter to ScriptBlock itself needs to be of type ScriptBlock. 啊,ScriptBlock本身的参数需要是ScriptBlock类型。
full code: 完整代码:
InitialSessionState initial = InitialSessionState.CreateDefault();
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddCommand("invoke-command");
ps.AddParameter("ComputerName", "mycomp.mylab.com");
ScriptBlock filter = ScriptBlock.Create("Get-childitem C:\\windows");
ps.AddParameter("ScriptBlock", filter);
foreach (PSObject obj in ps.Invoke())
{
// Do Something
}
Putting the answer here if someone finds it useful in the future 如果有人发现将来有用,请在此处给出答案
scriptblock字符串应与“{...}”格式匹配。使用以下代码即可:
ps.AddParameter("ScriptBlock", "{ get-childitem C:\\windows }");
你使用短格式:
ps.AddParameter("ScriptBlock", ScriptBlock.Create("Get-childitem C:\\Windows"));
An alternative approach in case it maybe more appropriate in some cases. 如果在某些情况下可能更合适,则采用另一种方法。
var remoteComputer = new Uri(String.Format("{0}://{1}:5985/wsman", "HTTP", "ComputerName"));
var connection = new WSManConnectionInfo(remoteComputer, null, TopTest.GetCredential());
var runspace = RunspaceFactory.CreateRunspace(connection);
runspace.Open();
var powershell = PowerShell.Create();
powershell.Runspace = runspace;
powershell.AddScript("$env:ComputerName");
var result = powershell.Invoke();
https://blogs.msdn.microsoft.com/schlepticons/2012/03/23/powershell-automation-and-remoting-ac-love-story/ https://blogs.msdn.microsoft.com/schlepticons/2012/03/23/powershell-automation-and-remoting-ac-love-story/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.