14 Replies Latest reply on Jul 1, 2020 7:46 AM by LucD

    PowerCLI Script to Gather VM Guest Network Information Using get-view

    BrianDGeorge Enthusiast

      Just can't get this to work right.  Name, IP, MacAddress, VlanID, and Network Adapter are fine but Gateway, SubnetMask, and DNS are all blanks. Any help would be appreciated.

       

      Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} |

      Select Name,

      @{N='IPAddress';E={$_.Guest.ipAddress}},

      @{N='Gateway';E={$nicInfo = Get-VMGuestNetworkInterface -VM $_ [string]::Join(',',($nicInfo | Select -ExpandProperty DefaultGateway))}},

      @{N='SubnetMask';E={[string]::Join(‘,’,($subnetmask))}},

      @{N='DNS';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.DnsConfig.IpAddress))}},

      @{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},  

      @{N='VlanId';E={$folder = Get-Folder VM

              $folderView = Get-View $folder -Property "[VirtualMachine]ChildEntity.Network.*"

              $folderView.LinkedView.ChildEntity[0].LinkedView.Network | Select-Object -ExpandProperty Name}},

      @{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} | Out-GridView

        • 1. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Can you try like this

           

          Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} |

          Select Name,

          @{N='IPAddress';E={$_.Guest.ipAddress}},

          @{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},

          @{N='SubnetMask';E={

              $ipAddr = $_.Guest.ipAddress

              @(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{

                  [IPAddress]$ip = 0;

                  $ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))

                  $ip.IPAddressToString

              }) -join '|'

          }},

          @{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},

          @{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},

          @{N='VlanId';E={$folder = Get-Folder VM

                  $folderView = Get-View $folder -Property "[VirtualMachine]ChildEntity.Network.*"

                  $folderView.LinkedView.ChildEntity[0].LinkedView.Network | Select-Object -ExpandProperty Name}},

          @{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} |

          Out-GridView

           

          ---------------------------------------------------------------------------------------------------------

           

          Was it helpful? Let us know by completing this short survey here.

           

           

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
            BrianDGeorge Enthusiast

            I really cannot thank you enough.  You have saved my bacon on several of these.

            • 3. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
              markshannon Novice

              Hi,

               

              this script is great - the only issue seems to be that it displays the same vlanid for all the vms?

               

              how do i overcome this please?

               

              thanks in advance

              mark

              • 4. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                LucD Guru
                Community WarriorsUser ModeratorsvExpert

                There is indeed an issue in that code.

                Try with this updated version

                 

                Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} -PipelineVariable vm |

                Select Name,

                @{N='IPAddress';E={$_.Guest.ipAddress}},

                @{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},

                @{N='SubnetMask';E={

                    $ipAddr = $_.Guest.ipAddress

                    @(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{

                        [IPAddress]$ip = 0;

                        $ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))

                        $ip.IPAddressToString

                    }) -join '|'

                }},

                @{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},

                @{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},

                @{N='VlanId';E={

                        $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"

                        ($folder.LinkedView.ChildEntity.where({$vm.MoRef -eq $_.MoRef})).LinkedView.Network.Name}},

                @{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} |

                Out-GridView

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                1 person found this helpful
                • 5. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                  markshannon Novice

                  this worked a treat - thanks once again

                  • 6. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                    bloomk Lurker
                    VMware Employees

                    Hey there!

                     

                    First off, thank you so much for all these scripts you put together for all of us!  It is greatly appreciated!  With this script below you provided, would it be difficult to add what host the VM is currently running on as well?

                     

                    Thanks again,

                     

                    Kevin

                    • 7. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                      LucD Guru
                      Community WarriorsUser ModeratorsvExpert

                      The following calculated property should do the trick,

                       

                      @{N='VMHost';E={(Get-View -Id $_.Runtime.Host -Property Name).Name}}

                       

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                        bloomk Lurker
                        VMware Employees

                        That did it!  Thanks again!  Cheers

                        • 9. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                          winsolo Novice

                          Hey LucD

                           

                          Could we add another hashtable with "networkname" of the filtered VM with its network in which the vNIC is connected with? I can't find a way to pull that info from the VM ExtensionData.Network Property. Although I can get the network name from Get-NetworkAdapter -VM $myvm, I don't know how this can be added to the existing script. Could we also do foreach to get the entire script run only for certain VMs, instead of running it against all the powered on VMs with -Filter @{'Runtime.PowerState'='poweredOn'} ?

                          • 10. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                            LucD Guru
                            vExpertCommunity WarriorsUser Moderators

                            The NetworkName that you get with Get-NetworkAdapter should be present in the VlanId property in the output.

                            Just be aware that this script will only work correctly when a VM has only 1 vNIC.

                             

                            There is no need to have a ForEach to report on specific VMs.

                            You can do that with the Filter hash table.

                            For example

                             

                            Get-View -ViewType VirtualMachine -Filter @{'Name'='^VM1$|^VM2$';'Runtime.PowerState'='poweredOn'} |

                             

                            will only return the VMs whose names match exactly VM1 and VM2.

                            The right-hand side of an entry in the Filter hash table is a RegEx expression.

                            So you can use Anchors (^ and $) and the 'or' operator.

                             

                            You can even construct that expression automatically.

                            Assume you have a .txt file with VM names, one per line.

                            Then you could do:

                             

                            $strReg = (Get-Content -Path .\vmnames.txt | %{"^$_$"}) -join '|'

                            Get-View -ViewType VirtualMachine -Filter @{'Name'=$strReg;'Runtime.PowerState'='poweredOn'} |

                            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                            • 11. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                              winsolo Novice

                              Get-View -ViewType VirtualMachine -Filter @{'Name'='^testvm$';'Runtime.PowerState'='poweredOn'} | Select Name,

                              @{N='VMHost';E={(Get-View -Id $_.Runtime.Host -Property Name).Name}},

                              @{N='IPAddress';E={$_.Guest.ipAddress}},

                              @{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},

                              @{N='SubnetMask';E={

                                  $ipAddr = $_.Guest.ipAddress

                                  @(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{

                                      [IPAddress]$ip = 0;

                                      $ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))

                                      $ip.IPAddressToString

                                  }) -join '|'

                              }},

                              @{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},

                              @{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},

                              @{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}},

                              @{N='VlanId';E={

                                      $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"

                                      ($folder.LinkedView.ChildEntity.where({$vm.MoRef -eq $_.MoRef})).LinkedView.Network.Name}} |

                              FL *

                               

                              The above code produces the output with "VlanId" being empty, although the target VM has only 1 vNIC. However, I removed all the other hashtables and ran the following code and this one worked. This code is able to give me the network name.

                               

                              Get-View -ViewType VirtualMachine -Filter @{'Name'='^testvm$';'Runtime.PowerState'='poweredOn'} -PipelineVariable vm | Select Name,

                              @{N='VlanId';E={

                                      $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"

                                      ($folder.LinkedView.ChildEntity.where({$vm.MoRef -eq $_.MoRef})).LinkedView.Network.Name}}

                              | FL *

                               

                              I don't know where the problem is.

                              • 12. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                                winsolo Novice

                                Sorry I figured it out.

                                 

                                I forgot the PipelineVariable Parameter. Once added, everything worked. Thanks again LucD

                                • 13. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                                  LucD Guru
                                  Community WarriorsUser ModeratorsvExpert

                                  The PipelineVariable parameter is missing

                                   

                                  Get-View -ViewType VirtualMachine -Filter @{'Name'='^testvm$';'Runtime.PowerState'='poweredOn'} -PipelineVariable vm |

                                   

                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                  1 person found this helpful
                                  • 14. Re: PowerCLI Script to Gather VM Guest Network Information Using get-view
                                    LucD Guru
                                    vExpertCommunity WarriorsUser Moderators

                                    Seems our entries crossed

                                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz