VMware Cloud Community
SureshSubbu
Contributor
Contributor
Jump to solution

List all VM's and associated PortGroups

I am looking for a PowerCLI command to get the following report

To list all VM's in a DC , the report shall contain VM-name, CPU , Memory, Disk Size, GuestOS, Port Group Name, Vlan ID, Cluster, Host name

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Between -VMHost and $_.VMHost there should be a blank.
Probably disappeared during the copy/paste.

The file I attached is correct.


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

View solution in original post

14 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VM |

Select Name,NumCPU,MemoryGB,UsedSpaceGB,

    @{N='GuestOS';E={$_.Guest.OSFUllName}},

    @{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},

    @{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},

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

    @{N='HostName';E={$_.VMHost.Name}}


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

SureshSubbu
Contributor
Contributor
Jump to solution

Hi Luc, Thanks for the script

I executed the script and getting errors, attached the error message. Could you please check

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you connected to your vCenter (COnnect-VIServer)?
How do you run the script? From a .ps1 file?


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

0 Kudos
Zsoldier
Expert
Expert
Jump to solution

$VMs = get-VM

$Report = @()

Foreach ($VM in $VMs) {

$NewObj = "" | Select VMName, CPU, Memory, DiskSize, GuestOS, PGName, VLANID, Cluster, HostName

$NewObj.VMName = $VM.Name

$NewObj.CPU = $VM.NumCPU

$NewObj.Memory = $VM.MemoryGB # Or swap out w/ $VM.MemoryMB if you want

$NewObj.DiskSize = $VM.ProvisionedSpaceGB # If you want just used $VM.UsedSpaceGB

$PG = $VM | Get-VirtualPortGroup

If ($PG.count -gt 1)

{

$PortGroups = $null

$VLANIDs = $null

     Foreach ($Portgroup in $PG)

          {

          $PortGroups = $Portgroup.Name + " | " + $PortGroups

          $VLANIDs = $Portgroup.VLANID + " | " + $VLANIDs

          }

$NewObj.PGName = $PortGroups.trimend("|")

$NewObj.VLANIDs = $VLANIDs.trimend("|")

}

Else {

$NewObj.PGName = $PG.Name

$NewObj.VLANID = $PG.VLANID

}

$NewObj.GuestOS = $VM.Guestid

$NewObj.Cluster = $VM.vmhost.parent.name

$NewObj.hostname = $VM.vmhost.name

$Report += $NewObj

}

$Report

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
SureshSubbu
Contributor
Contributor
Jump to solution

Hi Luc

I connected to vCenter server and I can execute the command , the output is appears like this.

We need to export the report to .CSV file in location (ex: C:\test\report.csv) , Could you please provide a script for report.

Name            : GOCCTRAF2_2_ENV2

NumCpu          : 4

MemoryGB        : 8

UsedSpaceGB     : 28.461758323945105075836181641

GuestOS         : Microsoft Windows Server 2016 (64-bit)

PortgroupName   : OCS-V1A-PG

PortgroupVlanId :

Cluster         : OCC-Cluster01

HostName        : 10.16.48.69

Name            : TVS-Web02

NumCpu          : 4

MemoryGB        : 8

UsedSpaceGB     : 0

GuestOS         :

PortgroupName   : CCTV-PG

PortgroupVlanId :

Cluster         : OCC-Cluster01

HostName        : 10.16.48.69

0 Kudos
SureshSubbu
Contributor
Contributor
Jump to solution

Hi Luc

I connected to vCenter server and I can execute the command , the output is appears like this.

We need to export the report to .CSV file in location (ex: C:\test\report.csv) , Could you please provide a script for report.

Name            : GOCCTRAF2_2_ENV2

NumCpu          : 4

MemoryGB        : 8

UsedSpaceGB     : 28.461758323945105075836181641

GuestOS         : Microsoft Windows Server 2016 (64-bit)

PortgroupName   : OCS-V1A-PG

PortgroupVlanId :

Cluster         : OCC-Cluster01

HostName        : 10.16.48.69

Name            : TVS-Web02

NumCpu          : 4

MemoryGB        : 8

UsedSpaceGB     : 0

GuestOS         :

PortgroupName   : CCTV-PG

PortgroupVlanId :

Cluster         : OCC-Cluster01

HostName        : 10.16.48.69

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, just send the result over the pipeline to Export-Csv

Get-VM |

Select Name,NumCPU,MemoryGB,UsedSpaceGB,

    @{N='GuestOS';E={$_.Guest.OSFUllName}},

    @{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},

    @{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},

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

    @{N='HostName';E={$_.VMHost.Name}} |

Export-Csv -Path C:\Test\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
SureshSubbu
Contributor
Contributor
Jump to solution

Hi Luc

The report is working, VLANID field is showing empty.

PortgroupName PortgroupVlanId Cluster HostName

OCS-V1A-PG Cluster01 10.16.2.140

OCS-V1A-PG Cluster02 10.16.48.69

CCTV-PG Cluster02 10.16.48.69

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you attach the script as you are running it?

It might be that something went wrong with your copy/paste.


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

0 Kudos
SureshSubbu
Contributor
Contributor
Jump to solution

Here is the script which I am running

PowerCLI C:\> Get-VM |

>> Select Name,NumCPU,MemoryGB,UsedSpaceGB,

>>     @{N='GuestOS';E={$_.Guest.OSFUllName}},

>>     @{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},

>>     @{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost$_.VMHost).VlanId -join '|'}},

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

>>     @{N='HostName';E={$_.VMHost.Name}} |

>> Export-Csv -Path C:\Test\report.csv -NoTypeInformation -UseCulture

PowerCLI C:\>

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're missing a blank in here -VMHost $_.VMHost


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

0 Kudos
SureshSubbu
Contributor
Contributor
Jump to solution

LUC, I am not catching your point, What I am missing in the script

Get-VM |

Select Name,NumCPU,MemoryGB,UsedSpaceGB,

@{N='GuestOS';E={$_.Guest.OSFUllName}},

@{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},

@{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},

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

@{N='HostName';E={$_.VMHost.Name}} |

Export-Csv -Path C:\Test\report.csv -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Between -VMHost and $_.VMHost there should be a blank.
Probably disappeared during the copy/paste.

The file I attached is correct.


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

SureshSubbu
Contributor
Contributor
Jump to solution

Hi Luc

Many thanks the script is working perfectly, this is the final script I am using. (Included few additional fields)

Connect-VIServer -Server 10.16.48.80 -User administrator@vsphere.local -Password r2020Rta18$

Get-VM |

Select Name,PowerState,Version,NumCPU,MemoryGB,ProvisionedSpaceGB,UsedSpaceGB,

    @{N='GuestOS';E={$_.Guest.OSFUllName}},

    @{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},

    @{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},

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

    @{N='HostName';E={$_.VMHost.Name}} |

Export-Csv -Path C:\Test\report.csv -NoTypeInformation -UseCulture

0 Kudos