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
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}}
}
}
}
}
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
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
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}}
}
}
}
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
Yeah.. Now its fine .. Is it possible to have only IPv4 ? Now I have IPv6 and IPv4 on seperate lines for the same host.
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}}
}
}
}
}
Great.
Really appriciate your efforts.
Regards
Rajesh
Great, exactly what I need. thanks.
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}}