14 Replies Latest reply on Mar 26, 2018 7:48 AM by SureshSubbu

    List all VM's and associated PortGroups

    SureshSubbu Novice

      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. Re: List all VM's and associated PortGroups
          LucD Guru
          vExpertUser ModeratorsCommunity Warriors

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          1 person found this helpful
          • 2. Re: List all VM's and associated PortGroups
            SureshSubbu Novice

            Hi Luc, Thanks for the script

             

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

            • 3. Re: List all VM's and associated PortGroups
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

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

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: List all VM's and associated PortGroups
                Zsoldier Expert
                vExpert

                $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

                • 5. Re: List all VM's and associated PortGroups
                  SureshSubbu Novice

                  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
                    SureshSubbu Novice

                    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
                      LucD Guru
                      User ModeratorsCommunity WarriorsvExpert

                      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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: List all VM's and associated PortGroups
                        SureshSubbu Novice

                        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
                          LucD Guru
                          User ModeratorsvExpertCommunity Warriors

                          Can you attach the script as you are running it?

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

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: List all VM's and associated PortGroups
                            SureshSubbu Novice

                            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
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

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

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              • 12. Re: List all VM's and associated PortGroups
                                SureshSubbu Novice

                                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
                                  LucD Guru
                                  User ModeratorsCommunity WarriorsvExpert

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

                                   

                                  The file I attached is correct.

                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                  1 person found this helpful
                                  • 14. Re: List all VM's and associated PortGroups
                                    SureshSubbu Novice

                                    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