Skip to content

Instantly share code, notes, and snippets.

@RezaAmbler
Created November 27, 2024 18:07
Show Gist options
  • Select an option

  • Save RezaAmbler/7095ff779e07ceb2a8e215c273e27d2f to your computer and use it in GitHub Desktop.

Select an option

Save RezaAmbler/7095ff779e07ceb2a8e215c273e27d2f to your computer and use it in GitHub Desktop.
PowerCLI VMWare Report Script
# 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" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment