Automation

 View Only
  • 1.  ipaddress and mac address issue

    Posted Jan 31, 2023 11:29 AM
      |   view attached

    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 

    Attachment(s)

    txt
    code.txt   26 KB 1 version


  • 2.  RE: ipaddress and mac address issue

    Posted Jan 31, 2023 11:59 AM

    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]
    

     



  • 3.  RE: ipaddress and mac address issue

    Posted Jan 31, 2023 11:59 AM

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



  • 4.  RE: ipaddress and mac address issue
    Best Answer

    Posted Jan 31, 2023 02:35 PM

    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