Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How to model output objects returned by Powershell scripts in C#?

$
0
0

 using (PowerShell PowerShellInstance = PowerShell.Create()){

             PowerShellInstance.AddScript("param($path1,$path2) $first = Get-ChildItem -Path $path1 -Filter *.dll\n $second =  Get-ChildItem -Path $path2 -Filter *.dll\n $diff=Compare-Object -ReferenceObject $first -DifferenceObject $second -Property Name, Length, LastWriteTime\n \nOut-String -InputObject $diff");

                 PowerShellInstance.AddParameter("path1", "D:\\Site1\\Dlls");

                PowerShellInstance.AddParameter("path2", "D:\\Site2\\Dlls");      

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

                outputCollection.DataAdded += outputCollection_DataAdded;

                PowerShellInstance.Streams.Error.DataAdded += Error_DataAdded;

                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");

                }

                foreach (PSObject outputItem in outputCollection)

                {

                    Console.WriteLine(outputItem.BaseObject.ToString());

                }


Viewing all articles
Browse latest Browse all 6937

Trending Articles