PowerCLI

 View Only
Expand all | Collapse all

List all VM's and associated PortGroups

  • 1.  List all VM's and associated PortGroups

    Posted Mar 22, 2018 01:32 PM

    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



  • 2.  RE: List all VM's and associated PortGroups

    Posted Mar 22, 2018 01:44 PM

    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}}



  • 3.  RE: List all VM's and associated PortGroups

    Posted Mar 22, 2018 02:04 PM

    Hi Luc, Thanks for the script

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



  • 4.  RE: List all VM's and associated PortGroups

    Posted Mar 22, 2018 02:10 PM

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



  • 5.  RE: List all VM's and associated PortGroups

    Posted Mar 25, 2018 12:19 PM

    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



  • 6.  RE: List all VM's and associated PortGroups

    Posted Mar 25, 2018 12:33 PM

    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



  • 7.  RE: List all VM's and associated PortGroups

    Posted Mar 25, 2018 04:31 PM

    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



  • 8.  RE: List all VM's and associated PortGroups

    Posted Mar 26, 2018 11:18 AM

    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



  • 9.  RE: List all VM's and associated PortGroups

    Posted Mar 26, 2018 11:25 AM

    Can you attach the script as you are running it?

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



  • 10.  RE: List all VM's and associated PortGroups

    Posted Mar 26, 2018 12:08 PM

    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:\>



  • 11.  RE: List all VM's and associated PortGroups

    Posted Mar 26, 2018 12:13 PM

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



  • 12.  RE: List all VM's and associated PortGroups

    Posted Mar 26, 2018 12:24 PM

    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



  • 13.  RE: List all VM's and associated PortGroups
    Best Answer

    Posted Mar 26, 2018 12:27 PM

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

    The file I attached is correct.



  • 14.  RE: List all VM's and associated PortGroups

    Posted Mar 22, 2018 02:12 PM

    $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



  • 15.  RE: List all VM's and associated PortGroups

    Posted Mar 26, 2018 02:48 PM

    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