Home > Software engineering >  Trying to output a custom powershell object where I can align each line of two different variables c
Trying to output a custom powershell object where I can align each line of two different variables c

Time:04-02

I'm trying to do an network access control audit by grabbing a user's AD groups, their descriptions and then output them in a way shown by this example:


[User]

@[1]Groups : @[1]GroupDescription @[2]...
@[3]...


Below is what I have at the moment.

$UserGroups = @{

User = Read-Host -Prompt "What user do You want to look up Access for?"
Groups = (Get-ADUser $User -Properties MemberOf).MemberOf
GroupsDescriptions = (Get-ADUser $User -Properties MemberOf).MemberOf | % {(Get-ADGroup $_ -Properties *).description}
}

$Object = New-Object psobject -Property $UserGroups

$Object | format-table | Export-Csv c:\tmp\test.csv

Though the output is very strange. I don't understand it. Below is a result of Get-Content C:tmp\test.csv

#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatStartData "ClassId2e4f51ef21dd47e99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry" "033ecb2bc07a4d43b5ef94ed5a35d280",,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo", "9e210fe47d09416682b841769c78b8a3",,,,, "27c87ef9bbda4f709f6b4002fa4af63c",,,,, "4ec4f0187cb04f4cb6973460dfe252df",,,,, "cf522b78d86c486691226b40aa69e95c",,,,,

I have tried outputting to a .txt file using Out-file, but I always get each property cut off with a ... at the end. I've used the -Autosize and -Expand when formatting the data before piping it to the export line.

Any Suggestions or advice would be extremely helpful.

Things I'll be Looking at later Go through each line in PowerShell object and extract variables Powershell & ActiveDirectory - trying to output users in a group and their membership Out-file crops my text when trying to output a table Thanks!

CodePudding user response:

  • As stated, only ever use Format-* cmdlets to produce for-display output, never for outputting data that must be processed programmatically later. What Format-Table outputs are objects representing formatting instructions, and it is their properties that ended up in your CSV file - see this answer for more information.

  • In order to include collections (arrays) in CSV output, you must convert them to a single string, using a self-chosen separator. Otherwise, Export-Csv simply calls .ToString() on the collection object itself, which yields the collection's type name, and no information about its elements.

Therefore, use something like the following, which uses ', ' as the separator string to represent the group names and descriptions in a single column each:

$UserGroups = [pscustomobject] @{
  User = ($user = Read-Host -Prompt "What user do You want to look up Access for?")
  Groups = ($groups = (Get-ADUser $User -Properties MemberOf).MemberOf) -join ', '
  GroupsDescriptions = (
      $groups | ForEach-Object { (Get-ADGroup $_ -Properties *).Description }
    ) -join ', '
}

$UserGroups | Export-Csv c:\tmp\test.csv

Note:

  • [pscustomobject] @{ ... } is used to directly construct a custom object, which is syntactic sugar available since PowerShell v3 that is simpler and more efficient than a New-Object call.

  • In order to use the result from your Read-Host call in later properties of your object definition, you must cache it in aux. variable $user (note that enclosing the assignment in (...) passes its value through.

  • Similarly, the result of the Get-ADUser call is cached in aux. variable $groups, so that it doesn't have to be repeated in the GroupsDescriptions value.

  • However, as zett42 points out, it may be cleaner to make the $user = ... and $groups = ... assignments separate statements and place them before the object construction.

CodePudding user response:

The problem is that you pipe to Format-Table before you pipe to Export-Csv. Only use Format-Table for displaying things on screen. The fix is to just remove that.

$Object | Export-Csv c:\tmp\test.csv
  • Related