如何在C#中为Powershell脚本返回的输出对象建模

如何在C#中为Powershell脚本返回的输出对象建模

本文介绍了如何在C#中为Powershell脚本返回的输出对象建模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using (PowerShell PowerShellInstance = PowerShell.Create()){
                <b>PowerShellInstance.AddScript("param($path1,$path2) Compare-Object -ReferenceObject (Get-ChildItem -Path $path1 -Filter *.txt |Get-Content) -DifferenceObject  (Get-ChildItem -Path $path2 -Filter *.txt|Get-Content) ");</b>
                PowerShellInstance.AddParameter("path1", "D:\\abc");
               PowerShellInstance.AddParameter("path2", "D:\\def");

                PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();

                PowerShellInstance.Streams.Error.Clear();
                PowerShellInstance.Streams.Warning.Clear();
                IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);

                while (result.IsCompleted == false)
                {
                    Console.WriteLine("Waiting for pipeline to finish...");
                    Thread.Sleep(1000);

                }

                Console.WriteLine("Execution has stopped. The pipeline state: " + PowerShellInstance.InvocationStateInfo.State);

                Console.WriteLine(PowerShellInstance.Streams.Error.Count().ToString() + " error counts");

                foreach (var errorRecord in PowerShellInstance.Streams.Error)
                {
                   Console.WriteLine(errorRecord.ToString() + " -> error");
                }

             <b>   foreach (PSObject outputItem in outputCollection)
                {

                    Console.WriteLine(outputItem.BaseObject.ToString());
                }
</b>
                Console.WriteLine("Finished!");
            }

推荐答案




这篇关于如何在C#中为Powershell脚本返回的输出对象建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:57