VMware Cloud Community
Sonic01
Contributor
Contributor
Jump to solution

Problem exporting ALL VM's and details to CSV

Hi Guys,

As after reading a bunch of threads here and modifying PS examples provided I've made a script which works pretty well for me and exports all my VM's with a bunch of relevant info I need.

But I have 2 problems:

1. The script only exports VM's listed under a DC, if a lone host is added to the root of the VM the script will miss it and not report on the VM's on that host

2. The script does not display the VM's Cluster names, whenever I've tried to add this I get weird results, such as the VM being listed under every DC - or VM's that are NOT inside a cluster do not get displayed.

Here is my current script:

$report = foreach($dc in Get-Datacenter){

    Get-VM -Location $dc |

    Select @{N='VC';E={$_.Uid.Split('@')[1].Split(':')[0]}},

        @{N='Datacenter';E={$dc.Name}},

        Name,

        @{N='DNS Name';E={$_.ExtensionData.Guest.HostName}},

        @{N='Configured OS';E={$_.ExtensionData.Config.GuestFullName}},

        @{N='Running OS';E={$_.ExtensionData.Guest.GuestFullName}},

        @{N='Power State';E={$_.PowerState}},

        @{N="CPU's";E={$_.NumCpu}},

        @{N="RAM";E={$_.MemoryGB}},

        @{N='Allocated Space';E={[math]::Round((Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum),0)}},

        @{N="Used Space (inc vSwap)";E={[math]::Round(($_.UsedSpaceGB | measure-Object -Sum).Sum)}},

        @{N="Free Space";E={[math]::Round(($_.Extensiondata.Guest.Disk | Measure-Object -Property FreeSpace -Sum | Select -ExpandProperty Sum)/1GB,0)}},

        @{N='Datastores';E={(Get-View -Id $_.DatastoreIdList -Property Name).Name -join '|'}},

        @{N="IP's";E={$_.Guest.IPAddress -join '|'}},

        @{N="PortGroups";E={((Get-VirtualPortgroup -Name (Get-NetworkAdapter -VM $_).NetworkName) | Sort-Object -Unique) -join '|'}},

        @{N="VLAN's";E={((Get-VirtualPortgroup -Name (Get-NetworkAdapter -VM $_).NetworkName).VlanId | Sort-Object -Unique) -join '|'}},

        @{N='VM HW Version';E={$_.ExtensionData.config.version}},

        @{N='VMware Tools Status';E={$_.ExtensionData.Guest.ToolsVersionStatus}},

        @{N='VM Notes';E={$_.ExtensionData.config.Annotation}}

}

$report | Export-csv -Path "C:\vms.csv" -NoTypeInformation -UseCulture

I've tried playing around with the scripts mentioned in this thread but not had any luck:

get-vm cluster name

Any help would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm not sure I understand what you mean by "if a lone host is added to the root".

An ESXi node always is located under a Datacenter.


For the Clustername, you could do something like this

$report = foreach($dc in Get-Datacenter){

    Get-VM -Location $dc |

    Select @{N='VC';E={$_.Uid.Split('@')[1].Split(':')[0]}},

        @{N='Datacenter';E={$dc.Name}},

        @{N='Cluster';E={(Get-Cluster -VM $_).Name}},

        Name,

        @{N='DNS Name';E={$_.ExtensionData.Guest.HostName}},

        @{N='Configured OS';E={$_.ExtensionData.Config.GuestFullName}},

        @{N='Running OS';E={$_.ExtensionData.Guest.GuestFullName}},

        @{N='Power State';E={$_.PowerState}},

        @{N="CPU's";E={$_.NumCpu}},

        @{N="RAM";E={$_.MemoryGB}},

        @{N='Allocated Space';E={[math]::Round((Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum),0)}},

        @{N="Used Space (inc vSwap)";E={[math]::Round(($_.UsedSpaceGB | measure-Object -Sum).Sum)}},

        @{N="Free Space";E={[math]::Round(($_.Extensiondata.Guest.Disk | Measure-Object -Property FreeSpace -Sum | Select -ExpandProperty Sum)/1GB,0)}},

        @{N='Datastores';E={(Get-View -Id $_.DatastoreIdList -Property Name).Name -join '|'}},

        @{N="IP's";E={$_.Guest.IPAddress -join '|'}},

        @{N="PortGroups";E={((Get-VirtualPortgroup -Name (Get-NetworkAdapter -VM $_).NetworkName) | Sort-Object -Unique) -join '|'}},

        @{N="VLAN's";E={((Get-VirtualPortgroup -Name (Get-NetworkAdapter -VM $_).NetworkName).VlanId | Sort-Object -Unique) -join '|'}},

        @{N='VM HW Version';E={$_.ExtensionData.config.version}},

        @{N='VMware Tools Status';E={$_.ExtensionData.Guest.ToolsVersionStatus}},

        @{N='VM Notes';E={$_.ExtensionData.config.Annotation}}

}

$report | Export-csv -Path "C:\vms.csv" -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not sure I understand what you mean by "if a lone host is added to the root".

An ESXi node always is located under a Datacenter.


For the Clustername, you could do something like this

$report = foreach($dc in Get-Datacenter){

    Get-VM -Location $dc |

    Select @{N='VC';E={$_.Uid.Split('@')[1].Split(':')[0]}},

        @{N='Datacenter';E={$dc.Name}},

        @{N='Cluster';E={(Get-Cluster -VM $_).Name}},

        Name,

        @{N='DNS Name';E={$_.ExtensionData.Guest.HostName}},

        @{N='Configured OS';E={$_.ExtensionData.Config.GuestFullName}},

        @{N='Running OS';E={$_.ExtensionData.Guest.GuestFullName}},

        @{N='Power State';E={$_.PowerState}},

        @{N="CPU's";E={$_.NumCpu}},

        @{N="RAM";E={$_.MemoryGB}},

        @{N='Allocated Space';E={[math]::Round((Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum),0)}},

        @{N="Used Space (inc vSwap)";E={[math]::Round(($_.UsedSpaceGB | measure-Object -Sum).Sum)}},

        @{N="Free Space";E={[math]::Round(($_.Extensiondata.Guest.Disk | Measure-Object -Property FreeSpace -Sum | Select -ExpandProperty Sum)/1GB,0)}},

        @{N='Datastores';E={(Get-View -Id $_.DatastoreIdList -Property Name).Name -join '|'}},

        @{N="IP's";E={$_.Guest.IPAddress -join '|'}},

        @{N="PortGroups";E={((Get-VirtualPortgroup -Name (Get-NetworkAdapter -VM $_).NetworkName) | Sort-Object -Unique) -join '|'}},

        @{N="VLAN's";E={((Get-VirtualPortgroup -Name (Get-NetworkAdapter -VM $_).NetworkName).VlanId | Sort-Object -Unique) -join '|'}},

        @{N='VM HW Version';E={$_.ExtensionData.config.version}},

        @{N='VMware Tools Status';E={$_.ExtensionData.Guest.ToolsVersionStatus}},

        @{N='VM Notes';E={$_.ExtensionData.config.Annotation}}

}

$report | Export-csv -Path "C:\vms.csv" -NoTypeInformation -UseCulture


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

0 Kudos
Sonic01
Contributor
Contributor
Jump to solution

Thanks LucD, that worked! and so simple as well...

and yes ignore me on the first part, i had forgotten that hosts have to reside under a DC.

0 Kudos