PowerCLI

 View Only
Expand all | Collapse all

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

  • 1.  Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jul 30, 2019 05:55 PM

    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



  • 2.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue
    Best Answer

    Posted Jul 30, 2019 06:08 PM

    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.



  • 3.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jul 30, 2019 06:31 PM

    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}



  • 4.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jul 30, 2019 06:39 PM

    Try replacing the sleep with

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



  • 5.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jul 30, 2019 07:17 PM

    That gave me exactly what I needed.

    Thanks!



  • 6.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jun 29, 2020 09:55 AM

    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



  • 7.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jun 29, 2020 10:58 AM

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

            }}

    }



  • 8.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jun 29, 2020 06:28 PM

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



  • 9.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jun 29, 2020 06:37 PM

    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 $_



  • 10.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jun 30, 2020 05:15 AM

    Thanks LucD that clear things.



  • 11.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Jun 30, 2020 07:37 AM

    Thanks a lot LucD,

    for me that script is perfect.

    I would never get it done by myself!

    Thanks a lot

    Erich



  • 12.  RE: Get-VM | Get-NetworkAdapter | Select-Object Issue

    Posted Nov 21, 2022 01:25 AM

    This is very helpful, thank you

    do you think can you also include the gateway and subnet mask in this script for each nic