VMware Cloud Community
esxi1979
Expert
Expert
Jump to solution

Generic Powershell Question

Hello

This is not related to a specific Vmware powershell cmdlet question , a generic question on export to csv

Here is what i see when i run a powershell cmd

Get-MachineGroup XXX|select name,filters|Format-Table

Name         Filters

----         -------

XXX {Server1, Server2, Server3, Server4...}

As you can see the list is not complete

So i tried

Get-MachineGroup XXX|select name,filters |export-csv c:\list.csv

but now the list shows as below for the server name col

 

Filters
ST.PSModule.Protect.MachineGroups.DiscoveryFilter[]

Tried

Get-MachineGroup |select name, @{N="Objects";E={$_.Filters}}

Get-MachineGroup |select name, @{N="Objects";E={$_.ST.PSModule.Protect.MachineGroups.DiscoveryFilter}}

No luck as well.

Any suggestion Please , how can i export that array of list of servers

Thanks

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are correct, the Export-Csv can't handle arrays.

It then displays the type of the value.

Your solution is correct.

An alternative to transform an array into a singular value could be

Get-MachineGroup | Select  name,

    @{N="Filters";E={$_.Filters -join '|'}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

7 Replies
esxi1979
Expert
Expert
Jump to solution

LucD​ once mentioned .. trying to do what he did..

The Export-Csv cmdlet can handle properties that are arrays or complex objects correctly.

A way around this is to convert the array to a single string.

0 Kudos
esxi1979
Expert
Expert
Jump to solution

The PowerShell export-csv command needs a distinct property name (fieldname) for each value. This is precisely where the PSObject comes in handy. It is able to store a series of values for the property names with the values. It can store a large number of unique property names and values per object.

Array(s) have implicit labels for each value.

Still doing google

0 Kudos
esxi1979
Expert
Expert
Jump to solution

tried below no luck

$box = Get-MachineGroup

[pscustomobject]@{

name = $box.name

List = ($box.filters |Out-String).Trim()

}

0 Kudos
esxi1979
Expert
Expert
Jump to solution

No luck with below too

Get-MachineGroup | Select ‘Name’,@{Names1=’Filters’;Expression={[string]::join(“;”, ($_.Filters))}}

0 Kudos
esxi1979
Expert
Expert
Jump to solution

What i so far think is , below code is good,

Get-MachineGroup | Select  name, @{N="Filters";E={[string]::Join(",",$_.Filters)}}

Name                    Filters
----                    -------
My Machine             

ST.PSModule.Protect.MachineGroups.DiscoveryFilter

The reason it shows above is internal of this cmdlet .. is that correct ? or i am doing something wrong on array extraction

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are correct, the Export-Csv can't handle arrays.

It then displays the type of the value.

Your solution is correct.

An alternative to transform an array into a singular value could be

Get-MachineGroup | Select  name,

    @{N="Filters";E={$_.Filters -join '|'}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference