VMware Cloud Community
coco86
Contributor
Contributor
Jump to solution

Script to get multiple ips per vm?

Hi

I have several vms with 2 or even 3 vnics, each one with different ips, I am trying to get a list of the vm name, the different ips and the dns name, is this possible?

I found a script and modified some parts, but I only get the first IP.

$VMs = Get-VM
foreach ($VM in $VMs){
     $VM = Get-View $VM.ID
     $nm = $VM.name
    $hn = $VM.guest.hostname
    $ip = $VM.guest.ipaddress
    $vm | select @{Name = "Name"; Expression = {$nm}}, @{Name = "Hostname"; Expression = {$hn}}, @{Name = "IP"; Expression = {$ip}}
    }

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The only difference I see, is that my script fetches the IP addresses from the Guest object.

That is only filled in when the VMware Tools are installed.

Could that explain the difference ?

Perhaps try my script but then with $_.IpAddress as well instead of $_.Guest.IpAddress.


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Try it like this

$VMs = Get-VM
foreach ($VM in $VMs){
    $VM = Get-View $VM.ID
    $nm = $VM.name
    $hn = $VM.guest.hostname
    $ip = [string]::Join(',',($VM.guest.ipaddress))
    $vm | select @{Name = "Name"; Expression = {$nm}}, @{Name = "Hostname"; Expression = {$hn}}, @{Name = "IP"; Expression = {$ip}}
}


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

coco86
Contributor
Contributor
Jump to solution

Hi Luc

I am getting this error in some vms, also,only get the first ip of the vm.

Exception calling "Join" with "2" argument(s): "Value cannot be null.
Parameter name: value"
At C:\Users\admin\Documents\Untitled1.ps1:6 char:25
+     $ip = [string]::Join <<<< (',',($VM.guest.ipaddress))
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Also, I found this script

$IPaddresses = @()

foreach($vm in Get-VM){
    $vm.Guest.Nics | %{
        $row = "" | Select Name, IP, MAC
        $row.Name = $vm.Name
        $row.IP = [String]::Join(',',$_.IPAddress)
        $row.MAC = $_.MacAddress
        $IPaddresses += $row
    }
}

$IPaddresses

That shows me the ip addresses, but don´t know how to add the DNS name of the vm.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, to avoid the error when there is no IpAddress, you can do

$VMs = Get-VM
foreach ($VM in $VMs){
    $VM = Get-View $VM.ID
    $nm = $VM.name
    $hn = $VM.guest.hostname
    $ip = &{
        if($VM.guest.ipaddress){
            [string]::Join(',',($VM.guest.ipaddress))
        }
    }
    $vm | select @{Name = "Name"; Expression = {$nm}}, @{Name = "Hostname"; Expression = {$hn}}, @{Name = "IP"; Expression = {$ip}}
}

For VMs with more than 1 IP address, the IP addresses are listed, seperated by a comma.


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

coco86
Contributor
Contributor
Jump to solution

Hi Luc

I tried your script, but I still get the first IP and the rest gets lost. Checked the variables (with powergui) and that was correct, once it does the join, the rest got lost.

I managed to do it in this way:


foreach($vm in Get-VM){
    $vm.Guest | %{
        $row = "" | Select Name, IP, HostName
        $row.Name = $vm.Name
        $row.IP =[String]::Join(',',$_.IPAddress)
        $row.HostName = $vm.Guest.HostName
        $IPaddresses += $row
        }
}

but I don't know why works different of your script.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The only difference I see, is that my script fetches the IP addresses from the Guest object.

That is only filled in when the VMware Tools are installed.

Could that explain the difference ?

Perhaps try my script but then with $_.IpAddress as well instead of $_.Guest.IpAddress.


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

0 Kudos
coco86
Contributor
Contributor
Jump to solution

Will give it a shot...

Thanks for your help!

0 Kudos