VMware Cloud Community
javedkhansiddiq
Contributor
Contributor
Jump to solution

Need Vm details in JsonArray List Format

I want to Print ESXI/ VM's information in below format.

javedkhansiddiq_0-1687199016862.png

some how i was able to get 4 columns , but coloum-3 (vc_vm_list) i have requirement like Json ArrayList
vc_vm_list" : [ "server01.cloud.org", "edge-02-abc", "edge-02-abc" ],


below is my code which work fo4 rest 4 columns 

Get-VM | Select @{Name="bp_host";Expression={(Get-VMHost -VM $_).Name.Split('.')[0]}},@{Name="vc_vcenter_server";Expression={$_.Uid.Split('@')[1].Split('.')[0]}},@{Name="vc_vm_list";Expression={Get-VM $_}},@{Name="vc_cluster";Expression={Get-Cluster -VM $_}},@{Name='z_vcenter_vms';Expression={'enrichment'}} | ` Sort-Object -Property bp_host | Export-CSV -Path "{{ my_folder }}\\{{ my_esxi_file }}" -NoTypeInformation




Tags (3)
0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM |
Select-Object @{Name = "bp_host"; Expression = { $_.VMHost.Name.Split('.')[0] } },
@{Name = "vc_vcenter_server"; Expression = { ([uri]$_.ExtensionData.Client.ServiceUrl).Host}},
@{Name = "vc_vm_list"; Expression = { (Get-VM -Location $_.VMHost).Name | ConvertTo-Json } },
@{Name = "vc_cluster"; Expression = { (Get-Cluster -VM $_).Name } },
@{Name = 'z_vcenter_vms'; Expression = { 'enrichment' } } |
Sort-Object -Property bp_host |
Export-Csv -Path "{{ my_folder }}\\{{ my_esxi_file }}" -NoTypeInformation -UseCulture


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

View solution in original post

LucD
Leadership
Leadership
Jump to solution

Try like this

@{Name = "vc_vm_list"; Expression = { """$((Get-VM -Location $_.VMHost).Name -join '|')""" } },


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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM |
Select-Object @{Name = "bp_host"; Expression = { $_.VMHost.Name.Split('.')[0] } },
@{Name = "vc_vcenter_server"; Expression = { ([uri]$_.ExtensionData.Client.ServiceUrl).Host}},
@{Name = "vc_vm_list"; Expression = { (Get-VM -Location $_.VMHost).Name | ConvertTo-Json } },
@{Name = "vc_cluster"; Expression = { (Get-Cluster -VM $_).Name } },
@{Name = 'z_vcenter_vms'; Expression = { 'enrichment' } } |
Sort-Object -Property bp_host |
Export-Csv -Path "{{ my_folder }}\\{{ my_esxi_file }}" -NoTypeInformation -UseCulture


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

javedkhansiddiq
Contributor
Contributor
Jump to solution

This looks better

but if we can remove the “newline” (\nl, CR/LF) after each VM listed ?

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try replacing that line with

@{Name = "vc_vm_list"; Expression = { ((Get-VM -Location $_.VMHost).Name | ConvertTo-Json).Replace("`r`n",'') } },


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

0 Kudos
javedkhansiddiq
Contributor
Contributor
Jump to solution


Hi,

Can we also format the column as follows, put the entire entry in double-quotes and each item separated by a pipe

vc_vm_list
---------
"server01.cloud.org|edge-02-abc|edge-02-abc"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

@{Name = "vc_vm_list"; Expression = { """$((Get-VM -Location $_.VMHost).Name -join '|')""" } },


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