gpt4 book ai didi

c# - 运行多个 powershell 命令

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:32 35 4
gpt4 key购买 nike

当我在 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();
}

关于c# - 运行多个 powershell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378696/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com