运行多个电源外壳命令

本文关键字:外壳命令 电源 运行 | 更新日期: 2023-09-27 17:57:03

当我在 C# 中调用 powershell 命令时,如何运行诸如 set-adserversettings 之类的前体命令?现在它返回 0 个结果。

这是我正在使用的代码:

Command command1 = new Command("set-adserversettings");
CommandParameter parameter1 = new CommandParameter("viewentireforest", true);
command1.Parameters.Add(parameter1);
Command command2 = new Command("set-userphoto");
CommandParameter parameter2a = new CommandParameter("identity", tbxName.Text);
CommandParameter parameter2b = new CommandParameter("picturedata", displayedImage);
CommandParameter parameter2c = new CommandParameter("domaincontroller", "xx-xx-xx-01.xx.xx.xx.xxx");
CommandParameter parameter2d = new CommandParameter("confirm", false);
command2.Parameters.Add(parameter2a);
command2.Parameters.Add(parameter2b);
command2.Parameters.Add(parameter2c);
command2.Parameters.Add(parameter2d);
Pipeline pipeline = runspacee.CreatePipeline();
pipeline.Commands.Add(command1);
pipeline.Commands.Add(command2);
var exResults = pipeline.Invoke();

运行多个电源外壳命令

经过测试,它将按如下方式工作,您应该在不同的管道上运行这些命令,但要持续运行:

    List<Command> commandList = new List<Command>();
    Command command1 = new Command("set-adserversettings");
    CommandParameter parameter1 = new CommandParameter("viewentireforest", true);
    command1.Parameters.Add(parameter1);
    commandList.Add(command1);
    Command command2 = new Command("set-userphoto");
    CommandParameter parameter2a = new CommandParameter("identity", tbxName.Text);
    CommandParameter parameter2b = new CommandParameter("picturedata", displayedImage);
    CommandParameter parameter2c = new CommandParameter("domaincontroller", "xx-xx-xx-01.xx.xx.xx.xxx");
    CommandParameter parameter2d = new CommandParameter("confirm", false);
    command2.Parameters.Add(parameter2a);
    command2.Parameters.Add(parameter2b);
    command2.Parameters.Add(parameter2c);
    command2.Parameters.Add(parameter2d);
    commandList.Add(command2);
    Pipeline pipeline;
    Collection<PSObject> exResults = null;
    foreach (Command cmd in commandList)
    {   
        pipeline = runspacee.CreatePipeline();
        pipeline.Commands.Add(cmd);
        exResults = pipeline.Invoke();
    }