Insert command into invoke command in C #

I have a large number of teams and, most likely, will not change them all. Therefore, I want to turn command objects into code block objects and add their parameters to the call so that I can later 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 does anyone know how to do this?

The two lines that I noted do not work (do not compile), and I would like to know how I can fix them.

    ScriptBlock s=basis.CommandText);//<<<<this does not work, how can I fix this?

must load the command from the database into the script block so that later it can be used to execute the code and

    invokeScript.Parameters.Join(basis.Parameters); // and this does not work either?

It is an attempt to move parameters from the database to invokescript.

remote powershell #, ( ) .

@Keith hill

Console.WriteLine( "create 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();

, "", followign:            MaakActiveDirectoryOU = ( "New-ADOrganizationalUnit" );

        MaakActiveDirectoryOU.Parameters.Add("Name", KlantNaam);
        MaakActiveDirectoryOU.Parameters.Add("Path", WebConfigurationManager.AppSettings["SharedOU"].ToString());
        ExecuteRemote2(MaakActiveDirectoryOU);

, . - ?

+4
1

ScriptBlock. script, create (: ScriptBlock , ), :

var s = ScriptBlock.Create(command.CommandText);

, :

foreach (var p in basis.Parameters) {
    invokeScript.Parameters.Add(p.Name, p.Value);
}

Join Linq , Join. .

+4

Source: https://habr.com/ru/post/1609128/


All Articles