# Initialize an empty array to store the results $resultArray = @() # Retrieve a list of all VMs from vCenter $vmlist = Get-VM # Select 4 random VMs from the list for processing $testList = $vmlist | Get-Random -Count 4 # Loop through the randomly selected VMs $testList | ForEach-Object { $vm = $_ # Current VM in the loop # Retrieve all network adapters for the current VM Get-NetworkAdapter -VM $vm | ForEach-Object { $nic = $_ # Current network adapter in the loop # Extract network information for the NIC by matching its MAC address $nicInfo = $vm.Guest.Nics | Where-Object { $_.MacAddress -eq $nic.MacAddress } # Combine all IP addresses associated with the NIC into a comma-separated string $ipAddresses = $nicInfo.IPAddress -join ", " # Retrieve the network name associated with the NIC (using the non-deprecated property) $networkName = $nicInfo.Device.NetworkName # Create a custom object to hold the VM, NIC, MAC address, network name, and IP details $resultArray += [PSCustomObject]@{ "VM Name" = $vm.Name # Name of the virtual machine "NIC Name" = $nic.Name # Name of the network adapter "MAC Address" = $nic.MacAddress # MAC address of the network adapter "Network Name" = $networkName # Network name (vSwitch or port group) "IP Addresses" = $ipAddresses # IP addresses assigned to the NIC } } } # The compiled list is now stored in $resultArray for further use # Example: Display the results in a table format $resultArray | Format-Table -AutoSize # Examples of further usage: # Filter results to find VMs with names matching "DFW" # $resultArray | Where-Object { $_."VM Name" -like "*DFW*" } # Export the results to a CSV file # $resultArray | Export-Csv -Path "VM_NetworkDetails.csv" -NoTypeInformation # Search for a specific MAC address in the results # $resultArray | Where-Object { $_."MAC Address" -eq "00:50:56:94:bf:05" }