VMware Cloud Community
LPLAdmin
Enthusiast
Enthusiast
Jump to solution

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

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 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

11 Replies
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LPLAdmin
Enthusiast
Enthusiast
Jump to solution

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}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try replacing the sleep with

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LPLAdmin
Enthusiast
Enthusiast
Jump to solution

That gave me exactly what I needed.

Thanks!

Reply
0 Kudos
erichoge
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

kiranganap100
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
kiranganap100
Contributor
Contributor
Jump to solution

Thanks LucD that clear things.

Reply
0 Kudos
erichoge
Contributor
Contributor
Jump to solution

Thanks a lot LucD,

for me that script is perfect.

I would never get it done by myself!

Thanks a lot

Erich

Reply
0 Kudos
georgesefen
Contributor
Contributor
Jump to solution

This is very helpful, thank you

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

Tags (1)
Reply
0 Kudos