VMware Cloud Community
pephal481
Contributor
Contributor
Jump to solution

Hello, I'm trying to get a list of users and groups that are assigned to each vm on a viserver. Need help expanding System.Object[]

Hello, I'm trying to get a list of users and groups that are assigned to each vm on a viserver. When I run the following script, and have it output to the screen, I see the permissions for each vm, but when I try to export to csv I get System.Object[] instead of a list of user accounts.

Any help would greatly be appreciated.

$results = @()

$fieldvms = get-vm -server $viserver

ForEach ( $vm in $fieldvms )

{

    $cluster = $vm | get-cluster

    $vmperms = $vm | Get-VIPermission

      

    $Properties = @{

    vm_name = $vm.Name

    cluster_name = $cluster.Name

    vm_perms = $vmperms.Where({$_.principal -like "corp*"}) | select Principal

   

    }

$Results += New-Object psobject -Property $properties

}

$Results | Select-Object VM_Name, Cluster_Name, VM_Perms |  Export-Csv path\file.csv

#Permissions output when trying to export to csv: System.Object[]

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The Export-Csv cmdlet can't handle row elements that are arrays.
In that case it shows this System.Object[], indicating it encountered an array.

A simple solution is to use a calculated property and in the Expression part join the elements of the array together into one string.


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The Export-Csv cmdlet can't handle row elements that are arrays.
In that case it shows this System.Object[], indicating it encountered an array.

A simple solution is to use a calculated property and in the Expression part join the elements of the array together into one string.


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

Reply
0 Kudos
pephal481
Contributor
Contributor
Jump to solution

Thanks LucD. I also found that I could use -join ',' to get the output that I need.

Reply
0 Kudos