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!"); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)