VMware Cloud Community
RAJESH3311
Enthusiast
Enthusiast
Jump to solution

Get the IP Adddress of the VMs with VLan name

Dears,

Is there any script to get the list of VMs with its IP addresses and VLans ?

like VMName, IP Adderss, VLan Name

Regards

Rajesh

1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following modification of my previous script shows only IPv4 IP addresses:

Get-VM |
ForEach-Object {
 
$VM = $_
 
$VM | Get-VMGuest | Select-Object -ExpandProperty Nics |
 
ForEach-Object {
   
$Nic = $_
   
foreach ($IP in $Nic.IPAddress)
    {
     
if ($IP.Contains('.'))
      {     
       
"" | Select-Object -Property @{Name='VM';Expression={$VM.Name}},
          @{Name
='IPAddress';Expression={$IP}},
          @{Name
='NetworkName';Expression={$Nic.NetworkName}}
      }
    }
  }
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
9 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following PowerCLI command will give you a list of VM names, IP addresses and network names:

Get-VM | Select-Object -Property Name,@{Name='IPAddress';Expression={($_ | Get-VMGuest).IPAddress}},@{Name='NetworkName';Expression={($_ | Get-NetworkAdapter).NetworkName}}

Message was edited by: Robert van den Nieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
RAJESH3311
Enthusiast
Enthusiast
Jump to solution

Great.

Worked for me. Adding to that, if a VM has 2 or 3 NICs, is there any option to list out then as seperate lines ? Now the scripts displays all IPs for a VM on a single line.

Like

VMName IPAddress Network

COMP1 172.16.1.10 VLAN10

COMP1 172.16.2.10 VLAN20

COMP1 172.16.3.10 VLAN30

Regards

Rajesh

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following PowerCLI script will output each IP address on a seperate line:

Get-VM |
ForEach-Object {
 
$VM = $_
 
$VM | Get-VMGuest | Select-Object -ExpandProperty Nics |
 
ForEach-Object {
   
$Nic = $_
   
foreach ($IP in $Nic.IPAddress)
    {
     
"" | Select-Object -Property @{Name='VM';Expression={$VM.Name}},
         @{Name
='IPAddress';Expression={$IP}},
         @{Name
='NetworkName';Expression={$Nic.NetworkName}}
    }
  }
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Prakas
Enthusiast
Enthusiast
Jump to solution

The NetworkName property gives the network label of the adapter for me. I guess network label and VLAN ID are same in your case.

I got the VLAN id through this command

Get-VirtualPortGroup -VM vmname | Select VLanId

Reply
0 Kudos
RAJESH3311
Enthusiast
Enthusiast
Jump to solution

Yeah.. Now its fine .. Is it possible to have only IPv4 ? Now I have IPv6 and IPv4 on seperate lines for the same host.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following modification of my previous script shows only IPv4 IP addresses:

Get-VM |
ForEach-Object {
 
$VM = $_
 
$VM | Get-VMGuest | Select-Object -ExpandProperty Nics |
 
ForEach-Object {
   
$Nic = $_
   
foreach ($IP in $Nic.IPAddress)
    {
     
if ($IP.Contains('.'))
      {     
       
"" | Select-Object -Property @{Name='VM';Expression={$VM.Name}},
          @{Name
='IPAddress';Expression={$IP}},
          @{Name
='NetworkName';Expression={$Nic.NetworkName}}
      }
    }
  }
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
RAJESH3311
Enthusiast
Enthusiast
Jump to solution

Great.

Really appriciate your efforts.

Regards

Rajesh

Reply
0 Kudos
Rene72
Contributor
Contributor
Jump to solution

Great, exactly what I need. thanks.Smiley Happy

Reply
0 Kudos
Sasi1ts
Contributor
Contributor
Jump to solution

From this script

Get-VM -name xxxx |

ForEach-Object {

  $VM = $_

  $VM | Get-VMGuest | Select-Object -ExpandProperty Nics |

  ForEach-Object {

    $Nic = $_

    foreach ($IP in $Nic.IPAddress)

    {

      if ($IP.Contains('.'))

      {     

        "" | Select-Object -Property @{Name='VM';Expression={$VM.Name}},

          @{Name='IPAddress';Expression={$IP}},

          @{Name='NetworkName';Expression={$Nic.NetworkName}}

      }

    }

  }

}

I am able to get the IP addresses for multiple NICS. but network name is not coming like dvportgroup-xxxxxxx. Is there any command to get the correect VLAN id.

When you use below script able to get the IP address and VLan id. but out put in single line. we need more effort on out put csv file.

Get-VM -name xxxxxx | Select-Object -Property Name,@{Name='IPAddress';Expression={($_ | Get-VMGuest).IPAddress}},@{Name='NetworkName';Expression={($_ | Get-NetworkAdapter).NetworkName}}

Reply
0 Kudos