10 Replies Latest reply on Jun 30, 2020 12:37 AM by erichoge

    Get-VM | Get-NetworkAdapter | Select-Object  Issue

    LPLAdmin Novice

      The below script works fine except I want to add another column to the spreadsheet for the IP assigned to the adapter.  Is that possible?

       

      Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}},@{N="NIC";E={$_.Name}},@{N="Network";E={$_.NetworkName}} | Export-csv C:\temp\VMPortGroups.csv

        • 1. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          The following should work.

          BUT, there is an issue with some versions of VMware Tools, in that they obtain the same IP address for different NICs.

          You will see different MAC addresses, but all with the same IP.

          The only way to have the correct IP address in case a multi-NIC VM, is to query correctly inside the guest OS (think Invoke-VMScript).

           

          Get-VM | Get-NetworkAdapter |

          Select-Object @{N="VM";E={$_.Parent.Name}},

             @{N="NIC";E={$_.Name}},

             @{N="Network";E={$_.NetworkName}},

            MacAddress,

             @{N='IP';E={

             $vNIc = $_

             ($_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress }).IPAddress -join '|'

             }}

           

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

           

          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: Get-VM | Get-NetworkAdapter | Select-Object  Issue
            LPLAdmin Novice

            Thanks Luc,  That worked.  I'll just have to check for the duplicate IP's and correct.  With a correct csv that will let me run this code to change my VM's from a DVS Port Group to a Local Portgroup and through in a test.  Is there a way in this script without making it too complex to add a continue instead of a pause if the test connection fails?  The script stops you fix the issue manually.  Hit a key to continue where it left off?  I put in a Sleep for a couple minutes but it would be nice to wait until I'm ready for the script to continue.

             

            foreach($pg in Import-Csv -Path C:\temp\VMPortGroups.csv -UseCulture){

             

             

                Get-VM -Name $pg.VM | Get-NetworkAdapter -Name $pg.NIC |

             

             

                Set-NetworkAdapter -NetworkName $pg.Network -Confirm:$False

                if (Test-Connection $pg.IP -Count 1 -Quiet) {Write-Host ($pg.VM + " with IP " + $pg.IP + " pingable") -ForegroundColor Green}

                else {Write-Host ($pg.VM + " with IP " + $pg.IP + " NOT pingable") -Foreground Red

                sleep 180}

            • 3. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              Try replacing the sleep with

               

              Read-Host -Prompt "Press <Enter> to continue"

               

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              1 person found this helpful
              • 4. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
                LPLAdmin Novice

                That gave me exactly what I needed.

                 

                Thanks!

                • 5. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
                  erichoge Novice

                  Hello LucD,

                   

                  first of all...thanks a lot for all your scripts (help) so far.

                  I use a lot of your scripts day by day.

                   

                  Right now I struggle to extend that script.

                   

                  Original:

                  Get-VM | Get-NetworkAdapter |

                  Select-Object @{N="VM";E={$_.Parent.Name}},

                   

                     @{N="NIC";E={$_.Name}},

                     @{N="Network";E={$_.NetworkName}},

                   

                    MacAddress,

                   

                     @{N='IP';E={

                   

                     $vNIc = $_

                   

                     ($_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress }).IPAddress -join '|'

                   

                     }}

                   

                  I have the need to extend it with the clustername, hostname, network name from the host itself , where the VM is located and which distributed switch is used. Furthermore  I have more than one networkadapter connected.

                  All of it should be in one line.

                   

                  The list should be like that

                   

                         

                  ClusternameHostnameNetwork name HostVM nameNetwork adapter nameNetwork name VMIP VM
                  ClusterXXXHostXXXN_XXX 1 | N_XXX 2 | N_XXX3 | etc.VM_XXXNetwork adapter 1 | network adapater  | network adapter 3 | etc.Network name 1 | network name 2 | network name 3 | etc.IP 1 | IP 2 | IP 3 | etc.

                  I struggle over days to get it done, but my skills are not the best.

                  It would be greate if you could help me with that task.

                   

                  Thanks a lot

                  Erich

                  • 6. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
                    LucD Guru
                    User ModeratorsvExpertCommunity Warriors

                    Something like this?

                     

                    Get-Cluster -PipelineVariable cluster |

                    Get-VMHost -PipelineVariable esx |

                    Get-VM -PipelineVariable vm |

                    ForEach-Object -Process {

                        $vmNet = Get-NetworkAdapter -VM $vm


                        '' | Select-Object @{N='Cluster';E={$cluster.Name}},

                            @{N='VMHost';E={$esx.Name}},

                            @{N='Network Name Host';E={(Get-VirtualPortgroup -VMHost $esx).Name -join '|'}},

                            @{N="VM";E={$vm.Name}},

                            @{N="NIC";E={$vmNet.Name -join '|'}},

                            @{N="Network";E={$vmNet.NetworkName -join '|'}},

                            @{N='MacAddress';E={$vmNet.MacAddress -join '|'}},

                            @{N='IP';E={

                                ($vmNet | %{

                                    $vNIc = $_

                                    $_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress } |Select -ExpandProperty IPAddress

                                }) -Join '|'

                            }}

                    }

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    1 person found this helpful
                    • 7. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
                      kiranganap100 Lurker

                      Thanks LudD, I have few quires regarding the script.

                           1) I have not come across -PipelineVairable , googled it but did't find anything related to it. Can you please share any some documentation on it.

                           2) Below line in the script, if I put everything in one line. Finding hardtime to understand this  $_$_ . Please if you can elaborate on this.

                       

                      @{N='IP';E={($vmNet | %{$vNIc = $_$_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress } |Select -ExpandProperty IPAddress}) -Join '|'}}

                      • 8. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
                        LucD Guru
                        Community WarriorsvExpertUser Moderators

                        1. The PipelineVariable parameter is one of the Common Parameters.

                        It stores the value of the current pipeline object in a named variable.

                         

                        2. You can not place that in one line. The line breaks are required. An alternative is to place a semicolon between the two $_

                        Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                        • 9. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
                          kiranganap100 Lurker

                          Thanks LucD that clear things.

                          • 10. Re: Get-VM | Get-NetworkAdapter | Select-Object  Issue
                            erichoge Novice

                            Thanks a lot LucD,

                             

                            for me that script is perfect.

                            I would never get it done by myself!

                             

                            Thanks a lot

                             

                            Erich