none
C# with Powershell and PSCustomObject RRS feed

  • Question

  • Hi everyone,

    I have been working with Powershell for a while, and I'm trying to port some of these Powershell scripts to C#. I'm at a bit of a roadblock when it comes to cmdlets that return PSCustomObject because I don't see a way to read the NoteProperty values for those objects.

    In Powershell, I have code that works liks this: $list = Import-CSV C:\mycsvfile.csv
    Since I have the headers already in the csv file, I can access each element like so: $list.pname

    In this particular case, I'm using the Pipeline.Invoke() method which returns PSObject types. Because the Import-CSV cmdlet returns a PSCustomObject, I'm just not sure how to access those items.

    Here is the code that I've been noodling with:

    Runspace runSpace = RunspaceFactory.CreateRunspace();
    runSpace.Open();
    Pipeline pipeline = runSpace.CreatePipeline();
    Command importcsv = new Command("Import-CSV");
    importcsv.Parameters.Add("Path", @"C:\mycsvfile.csv");
    pipeline.Commands.Add(importcsv);
    Collection<PSObject> output = pipeline.Invoke();
    runSpace.Close();
    foreach (PSObject psObject in output)
    {
      listBox1.Items.Add(psObject);
    }
    
    

    When I do this, I can see that the data is there; my list box is populated with what looks like a Powershell hash array:
    @{pname=HPLJ1;dname=HP LaserJet 4000 Series PCL6}

    The problem is that I can't access elements of psObject like I can with the Powershell variable. psObject.pname won't compile, understandably, because it would have no way of knowing what NoteProperties will belong to that PSCustomObject.

    I hope I have explained the problem sufficiently. I would appreciate any insight you could lend me. What I'm essentially trying to do is be lazy and not write a CSV file parser in C# when I have this beautiful functionality already in Powershell. If only I knew how to make it work! Thank you for your help.


    Drew Epstein
    Monday, October 11, 2010 11:21 AM

Answers

  • Hi Ganellon,

    Welcome to MSDN Forums!

     

    I think you can use the PSObject.Properties to access the items in this collection.

    PSObject.Properties Property

                Runspace runSpace = RunspaceFactory.CreateRunspace();

                runSpace.Open();

                Pipeline pipeline = runSpace.CreatePipeline();

                Command importcsv = new Command("Import-CSV");

                importcsv.Parameters.Add("Path", @"D:\mycsvfile.csv");

                pipeline.Commands.Add(importcsv);

                Collection<PSObject> output = pipeline.Invoke();

                runSpace.Close();

                foreach (PSObject psObject in output)

                {

                    foreach (PSPropertyInfo psPropertyInfo in psObject.Properties)

                    {

                        Console.Write("name: " + psPropertyInfo.Name);

                        Console.Write("\tvalue: " + psPropertyInfo.Value);

                        Console.WriteLine("\tmemberType: " + psPropertyInfo.MemberType);

                    }

                }

     

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

     

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    • Marked as answer by Ganellon Wednesday, October 13, 2010 10:34 PM
    Wednesday, October 13, 2010 11:53 AM
    Moderator

All replies

  • Hi Ganellon,

    Welcome to MSDN Forums!

     

    I think you can use the PSObject.Properties to access the items in this collection.

    PSObject.Properties Property

                Runspace runSpace = RunspaceFactory.CreateRunspace();

                runSpace.Open();

                Pipeline pipeline = runSpace.CreatePipeline();

                Command importcsv = new Command("Import-CSV");

                importcsv.Parameters.Add("Path", @"D:\mycsvfile.csv");

                pipeline.Commands.Add(importcsv);

                Collection<PSObject> output = pipeline.Invoke();

                runSpace.Close();

                foreach (PSObject psObject in output)

                {

                    foreach (PSPropertyInfo psPropertyInfo in psObject.Properties)

                    {

                        Console.Write("name: " + psPropertyInfo.Name);

                        Console.Write("\tvalue: " + psPropertyInfo.Value);

                        Console.WriteLine("\tmemberType: " + psPropertyInfo.MemberType);

                    }

                }

     

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

     

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    • Marked as answer by Ganellon Wednesday, October 13, 2010 10:34 PM
    Wednesday, October 13, 2010 11:53 AM
    Moderator
  • Mike, this was exactly what I needed, and would not have found it without your help! Thank you so very much for your time and effort. I am delighted at the simplicity of the solution.
    Drew Epstein
    Wednesday, October 13, 2010 10:37 PM
  • Hi Ganellon,

    You’re welcome!

     

    If you have any difficulty in future programming, welcome back. Here are many enthusiastic community members to offer their help and suggestions.

     

    If there’s anything unclear, please feel free to let me know.

     

    Have a nice day!

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Thursday, October 14, 2010 2:41 AM
    Moderator