我有大量的命令,宁愿不改变它们全部。所以我想将命令对象变成代码块对象,并将它们的参数添加到调用中,以便稍后在另一台计算机上执行它们。将命令插入到c中的调用命令中#
最终的结果是下面的代码: 串服务器= cpu1.com
public void ExecuteRemote(Command basis) {
RunspaceInvoke invoke = new RunspaceInvoke();
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeLine = runspace.CreatePipeline();
ScriptBlock s=basis.CommandText);//<<<<this does not work, how can I fix this?
Command invokeScript = new Command("Invoke-Command");
invokeScript.Parameters.Add("ComputerName", server);
invokeScript.Parameters.Add("scriptBlock", s);
invokeScript.Parameters.Join(basis.Parameters); // and this does not work either?
pipeLine.Commands.Add(invokeScript);
Collection<PSObject> commandResults = pipeLine.Invoke();
}
所以有人知道如何做到这一点?
我标记的两行不起作用(不编译),我想知道如何修复它们。
ScriptBlock s=basis.CommandText);//<<<<this does not work, how can I fix this?
应加载从基础的命令插入到脚本块,因此它可以以后用于执行所述代码和
invokeScript.Parameters.Join(basis.Parameters); // and this does not work either?
是从基础移动参数插入invokescript的尝试。
我已经根据我的答案上Invoke remote powershell command from C#但我不能改变每个函数的参数(还有很多命令的工作波谷)我想只取指令并执行。后看着@Keith山的回答
Console.WriteLine( “创造援引”)
编辑; RunspaceInvoke invoke = new RunspaceInvoke();
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Console.WriteLine("creating pipeline");
Pipeline pipeLine = runspace.CreatePipeline();
Console.WriteLine("creating scriptblock");
ScriptBlock s = ScriptBlock.Create(basis.CommandText);
Command invokeScript = new Command("Invoke-Command");
Console.WriteLine("adding parameter1");
invokeScript.Parameters.Add("ComputerName", "test1.com");
invokeScript.Parameters.Add("scriptBlock", s);
foreach (var p in basis.Parameters) {
invokeScript.Parameters.Add(p.Name, p.Value);
}
pipeLine.Commands.Add(invokeScript); Console.WriteLine("res");
Collection<PSObject> commandResults = pipeLine.Invoke();
我得到的错误,没有参数可以通过名称时要求添加followign命令“名称”中找到: 命令MaakActiveDirectoryOU =新命令(“新ADOrganizationalUnit”);
MaakActiveDirectoryOU.Parameters.Add("Name", KlantNaam);
MaakActiveDirectoryOU.Parameters.Add("Path", WebConfigurationManager.AppSettings["SharedOU"].ToString());
ExecuteRemote2(MaakActiveDirectoryOU);
这个命令在本地执行时工作正常任何人都有线索?
为什么downvote?谨慎解释? – Thijser
现在我很好奇为什么两个人决定downvote,我认为这是一个合理的问题? – Thijser
我也不知道 - 这是一个很好的问题,你很清楚地列出了问题,你展示了你自己的研究成果,而且问题问得很好,而且英文完美(没有错别字,清晰的语言)。我upvoted至少补偿一个无法解释的downvote ..... –