VMware Cloud Community
plaman
Enthusiast
Enthusiast
Jump to solution

ipaddress and mac address issue

having a hard time getting the IP addresses , and mac  for VMs in  the attached script .

all the variables contain the correct data , but every now and then  the $PrimaryIPAddress  variable will store  a lot of ip address, so does the $MacAddress variable 

any idea will be appreciated 

Reply
0 Kudos
1 Solution

Accepted Solutions
plaman
Enthusiast
Enthusiast
Jump to solution

well, thank you.

you just pointed out the issue, 

I was not clearing the IP address and the mac address Arrays for the primary ip address list.

thank you 

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure what the issue is, your code in fact does that when more than 1 vNIC is present.

    if (-not([String]::IsNullOrWhiteSpace($PrimaryIPAddress))) {
        $PrimaryIPAddress = $PrimaryIPAddress -replace "([a-f0-9:]+:)+[a-f0-9]+", ""
        $PrimaryIPAddress = $PrimaryIPAddress.trim()
    }
    elseif ([String]::IsNullOrWhiteSpace($PrimaryIPAddress)) {
        $PrimaryIPAddress = "NULL"
    }
    if ($PrimaryIPAddress.Count -gt 1) {
        Write-Host "More than one IP" -BackgroundColor Red
        foreach ($IP in $PrimaryIPAddress) {
            if ([String]::IsNullOrWhiteSpace($PrimaryIPAddressList)) {
                $PrimaryIPAddressList += $IP
            }
            else { $PrimaryIPAddressList += "|" + $IP }
        }
        $PrimaryIPAddress = $PrimaryIPAddressList | Out-String
    }

Similar for the MAC address.

Also I don't really understand what the purpose of the following line is

    $PrimaryIPAddress = ($vm | Where-Object { $_.ExtensionData.Config.InstanceUuid -eq $InstanceUuid -and $_.ExtensionData.Config.Uuid -eq $VMUUID }).Guest.Ipaddress

When a VM has multiple vNICs, it can have multiple IP and MAC addresses.
If you mean by "Primary IP Address" the IP address of 'Network adapter 1', you could replace the $PrimaryIPAddress and $MacAddress code with

    $primaryNIC = $vm.ExtensionData.Config.Hardware.Device | Where-Object { $_ -is [VMware.Vim.VirtualEthernetCard] -and $_.DeviceInfo.Label -eq 'Network adapter 1' }
    $MacAddress = $primaryNIC.MacAddress
    $PrimaryIPAddress = $vm.Guest.Nics.where{$_.MacAddress -eq $MacAddress}.IpAddress[0]

 


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

Reply
0 Kudos
maksym007
Expert
Expert
Jump to solution

maybe a bit not for this topic but RVtools will give you a full excel report with IP and MAC

Reply
0 Kudos
plaman
Enthusiast
Enthusiast
Jump to solution

well, thank you.

you just pointed out the issue, 

I was not clearing the IP address and the mac address Arrays for the primary ip address list.

thank you 

Reply
0 Kudos