3

I'm trying to convert the output of a powershell (AWS Tools) command to strings so that I can export them to CSV or HTML. I for the life of me can't figure it out. I've seen comments on hashtables, naming elements, etc. Nothing seems to help me. (I'm very much a newbie).

This is what I got. This command

(Get-IAMAccountAuthorizationDetail).UserDetailList | Select UserName, Grouplist

Will output this (with better spacing):

UserName GroupList
-------- ---------
User1 {Admins,Test}
User2 {Admins}

I cant' seem to figure out how to get this data so that it can be converted to CSV or HTML. Those brackets are an indication its an object, array or something. Can someone show me the code that would convert this to text or something that the Convertto-CVS o Convertto-HTML commands would work.

The output (subset) of the Get-Member Command is this:

TypeName : Amazon.IdentityManagement.Model.UserDetail Name : Equals MemberType : Method Definition : bool Equals(System.Object obj)

TypeName : Amazon.IdentityManagement.Model.UserDetail Name : GetHashCode MemberType : Method Definition : int GetHashCode()

TypeName : Amazon.IdentityManagement.Model.UserDetail Name : GroupList MemberType : Property Definition : System.Collections.Generic.List[string] GroupList {get;set;}

Thanks

stewtenn
  • 45
  • 5
  • 1
    _(Get-IAMAccountAuthorizationDetail).UserDetailList | Select UserName, Grouplist | ConvertTo-Csv -NoTypeInformation_ is not working? I do not have _Get-IAMAccountAuthorizationDetail_ command but tried it with _Get-ChildItem_ and it works correctly. – Stano Peťko Jan 07 '20 at 19:03
  • 2
    You could do something like the following, which will create a semi-colon delimited list within the `GroupList` cell: `(Get-IAMAccountAuthorizationDetail).UserDetailList | Select UserName,@{n='GroupList';e={$_.Grouplist -join ';'}}` – AdminOfThings Jan 07 '20 at 19:18
  • No, the output to the GroupList field is ""System.Collections.Generic.List`1[System.String]"" – stewtenn Jan 07 '20 at 19:28
  • What did you try? – Amit Baranes Jan 07 '20 at 19:42
  • Admin of things -- That did it! - Almost I had tried similar things based on generic examples and they never worked, so thank you! Now... It still holds all the items int grouplist under one set of Quotes and treats it as 1 item. how can I make each item in grouplist its own :) – stewtenn Jan 07 '20 at 19:47
  • How would you add a second expression to this -- "@{n='GroupList';e={$_.Grouplist -join ';'}}" Say there was a second field that needed the same thing done to it. – stewtenn Jan 07 '20 at 21:58
  • @stewtenn, the syntax `@{n='Name';e={Expression}}` is called a calculated property. If you want to add another, just add a comma to the end of that line and use that syntax. – AdminOfThings Jan 08 '20 at 11:41

1 Answers1

2

You could do something like the following, which will create a semi-colon delimited list within the GroupList cell:

(Get-IAMAccountAuthorizationDetail).UserDetailList |
    Select-Object UserName,@{n='GroupList';e={$_.Grouplist -join ';'}}

Explanation:

The syntax @{n='Name';e={Expression}} is called a calculated property as explained at Select-Object. Here is some information about the calculated property:

  • It is a hash table with custom properties.
  • The first property is Name, which is a label for your expression output. n,Name,l, and label are all acceptable property names for that property.
  • The value passed to n is just a string that you are creating. It is the property name that will show up in your output, and it does not need to already exist in your object. Your actual property is called GroupList. As an example with n='All The Groups', the property name would becomeAll The Groups` in your output. There is nothing wrong with reusing the same name the property currently has.
  • The Expression or e is the ScriptBlock, which is why it is surrounded by {}. The ScriptBlock is responsible for producing the value in your custom property.
  • $_ is the current pipeline object passed into the ScriptBlock. This means if you have a collection (just like you do in your case), $_ will represent each of those items in order.
  • If you want to add another calculated property, just add a comma after the last and use the calculated property syntax like so:

    Select-Object @{n='CustomProperty1';e={$_.ObjectProperty1}},@{n='CustomProperty2';e={$_.ObjectProperty2}}
    
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27