Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行每个管道命令后,在C#中捕获PowerShell输出_C#_Wpf_Powershell - Fatal编程技术网

执行每个管道命令后,在C#中捕获PowerShell输出

执行每个管道命令后,在C#中捕获PowerShell输出,c#,wpf,powershell,C#,Wpf,Powershell,我正在实现一个WPF应用程序,它将为字典中给定的每个键/值对执行PowerShell脚本,并将该对用作脚本参数。我将脚本的每次运行作为新命令存储在管道中。但是,这导致我只能从上次运行的命令中获取输出,而每次运行脚本后都需要输出。我已经考虑过在每次执行脚本时创建一个新管道,但我需要知道脚本的所有执行何时完成。以下是帮助解释我的问题的相关代码: private void executePowerShellScript(String scriptText, Dictionary<String,

我正在实现一个WPF应用程序,它将为字典中给定的每个键/值对执行PowerShell脚本,并将该对用作脚本参数。我将脚本的每次运行作为新命令存储在管道中。但是,这导致我只能从上次运行的命令中获取输出,而每次运行脚本后都需要输出。我已经考虑过在每次执行脚本时创建一个新管道,但我需要知道脚本的所有执行何时完成。以下是帮助解释我的问题的相关代码:

private void executePowerShellScript(String scriptText, Dictionary<String, String> args)
{
     // Create the PowerShell object.
     PowerShell powerShell = PowerShell.Create();

     // If arguments were given, add the script and its arguments.
     if (args != null)
     {
          foreach (KeyValuePair<String, String> arg in args)
          {
               powerShell.AddScript(scriptText);
               powerShell.AddArgument(arg.Key);
               powerShell.AddArgument(arg.Value);
          }
     }

     // Otherwise, just add the script.
     else
          powerShell.AddScript(scriptText);

     // Add the event handlers.
     PSDataCollection<PSObject> output = new PSDataCollection<PSObject>();
     output.DataAdded += new EventHandler<DataAddedEventArgs>(Output_DataAdded);
     powerShell.InvocationStateChanged +=
          new EventHandler<PSInvocationStateChangedEventArgs>(Powershell_InvocationStateChanged);

     // Invoke the pipeline asynchronously.
     IAsyncResult asyncResult = powerShell.BeginInvoke<PSObject, PSObject>(null, output);
}

private void Output_DataAdded(object sender, DataAddedEventArgs e)
{
     PSDataCollection<PSObject> myp = (PSDataCollection<PSObject>)sender;

     Collection<PSObject> results = myp.ReadAll();
     foreach (PSObject result in results)
     {
          Console.WriteLine(result.ToString());
     }
}
因此,为了回答我的问题:

1)我可以强制管道在执行每个命令后生成输出吗?或者,
2)如果每次运行命令时都要创建一个新管道,是否有其他方法可以检查脚本的所有执行是否已完成


在C语言中使用实际的
PowerShell类的例子很少,我对线程几乎一无所知,因此非常感谢您的帮助。

我觉得回答自己的问题很傻,但我所做的只是将循环功能从C语言代码移到脚本中,这样做很有效。所以现在我一次将所有键和值作为数组参数传递,并且管道中只有一个命令


不过,我仍然想知道在管道中的每个命令执行后是否有可能生成输出。

我有一个管理PowerShell环境的对象,它接受一个脚本或一个模块和cmdlet并执行它,然后从原始模块中接受一个新的脚本或模块和cmdlet或cmdlet并执行它。每次执行某项操作时,我都需要返回结果。我通过在每次执行后清除命令队列来解决此问题:

powerShellInstance.Commands.Clear();
希望这有帮助

powerShellInstance.Commands.Clear();