Create custom object

This topic contains 6 replies, has 4 voices, and was last updated by  js 2 months, 1 week ago.

  • Author
  • #96095

    Vandrey Trindade


    I'm trying to create a custom object that has values from three different variables.
    Imagine this scenario:

    Variable1 content:

    Variable2 content:

    Variable3 content:

    I want to create an object that is something like this:
    "Local Admins";"Remote Desktop";"Logons"

    Is there a way?

  • #96098

    Don Jones

    Of course.

    $props = @{ColumnA = $var.Value1
               ColumnB = $var.Value2}
    New-Object -Type PSObject -Prop $props

    That's the basic format. Do that for each row you want to produce. If you need the final result to be delimited with semicolons, as in your example, collect those rows into a variable and:

    $AllRows | ConvertTo-CSV -Delim ";"

    And you'll get a semicolon-delimited set.

    • #96102

      Vandrey Trindade

      Don Jones,

      Wow, you're fast!
      But how can I do it when I don't know how many values are in those variables?
      For example: Variable1 can have 3 values, Variable2 can have 10 values and Variable3 can have 6 values...

  • #96104

    Don Jones

    Mmm, that's not really how objects work. Objects don't have variable numbers of properties. If your end goal is that example you provided, then that's not a good format for variable data. Meaning, if one machine can have multiple "Local Admins," PowerShell can represent that in an object, but it becomes hierarchical data. You can't represent hierarchical data in a flat file.

    Think of an Excel sheet. If you were to do this in an Excel sheet, how would you put multiple values into a single cell for "Local Admins" on one machine? Maybe you're thinking, "oh, I'd just do like commas between them," and that might be fine for you as a human, but it's a sucky data format for computers to deal with. That's why computers have relational databases instead of doing everything in CSV files or Excel sheets.

    So what's the end goal, here? What are you trying to get to?

    • #96174

      Vandrey Trindade

      Don Jones and postanote,

      I need to create a report of users that are in the local Admins, Remote Desktop users and who logged on multiple servers.
      I was able to create a TXT with that information, but I was trying to create an HTML with that info side by side.
      Microsoft auditing reasons.
      But that's ok, I think I'll try to use an Excel file to output the data...
      I'll take a look at those sites postanote!

      Thanks for your time and attention!!!

  • #96128


    Ditto to what DonJ stated here.

    Yet, you'd also might want to wrap you head around PoSH multi-dimensional arrays.

    See these discussions.

    Today I want to talk a little about multidimensional arrays in Windows Powershell. Multidimensional arrays are one of the complex data types supported by Powershell. They can be used to dynamically store information in a volatile table without having it written to a real database in a file on the disk.


    Easily Create and Manipulate an Array of Arrays in PowerShell

    Comparing two multi-dimensional arrays

  • #96182


    Hey Don, anything wrong with making objects this way?

    [pscustomobject]@{ColumnA = $var.Value1
                      ColumnB = $var.Value2}

You must be logged in to reply to this topic.