I have a large number of commands and would rather not change all of them. So I want to turn command objects into codeblock objects and add their parameters to an invoke so I can later on execute them on another computer.
The end result is the following code: string server=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();
}
So anybody know how to do this?
The two lines that I marked are not working (don't compile) and I would like to know how I could fix them.
ScriptBlock s=basis.CommandText);//<<<<this does not work, how can I fix this?
should load the command from basis into the scriptblock so it can later be used for executing the code and
invokeScript.Parameters.Join(basis.Parameters); // and this does not work either?
Is an attempt to move the parameters from basis into the invokescript.
I have based my answer on Invoke remote powershell command from C# however as I cannot change the parameters for each function (there are a lot of commands to work trough) I would like to just take the command and execute that.
Edit after looking at @Keith hill's answer
Console.WriteLine("creating invoke"); 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();
I get the error that no parameter could be found by the name "Name" when asked to add the followign command: Command MaakActiveDirectoryOU = new Command("New-ADOrganizationalUnit");
MaakActiveDirectoryOU.Parameters.Add("Name", KlantNaam);
MaakActiveDirectoryOU.Parameters.Add("Path", WebConfigurationManager.AppSettings["SharedOU"].ToString());
ExecuteRemote2(MaakActiveDirectoryOU);
This command works fine when executed locally anybody got a clue?