Reputation: 45
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
Upvotes: 3
Views: 666
Reputation: 25031
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:
Name
, which is a label for your expression output. n
,Name
,l
, and label
are all acceptable property names for that property.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 become
All The Groups` in your output. There is nothing wrong with reusing the same name the property currently has.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}}
Upvotes: 2