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

7
  • 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. Commented Jan 7, 2020 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 ';'}} Commented Jan 7, 2020 at 19:18
  • No, the output to the GroupList field is ""System.Collections.Generic.List`1[System.String]"" Commented Jan 7, 2020 at 19:28
  • What did you try? Commented Jan 7, 2020 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 :) Commented Jan 7, 2020 at 19:47

1 Answer 1

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}}
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.