So here is the script so far, however I don't know how to include the virtual machine name next to each result. I've tried to add $_.Name (see ** areas) but this doesn't work. I've also noticed another script extracted the MAC address and IP address of the VM, this would be ideal (again see **). Could someone please point me in the right direction to get that VM name info into the CSV file, Much Appreciated.
# Import VMware PowerCLI module Import-Module -Name VMware.VimAutomation.Core # Input the export CSV path $exportPath = "E:\VLAN_$(Get-Date -Format "yyMMdd_hh-mm-ss").csv" # vCenter Server credentials $vcServer = "vcenter" $vcUsername = "$vcUsername" $vcPassword = "pass" $credentials = New-Object System.Management.Automation.PSCredential ($vcUsername,(ConvertTo- SecureString $vcPassword -AsPlainText -Force)) # Connect to vCenter Connect-VIServer -Server $vcServer -Credential $credentials # Get all distributed and standard switches $distSwitches = Get-VDSwitch $standSwitches = Get-VirtualSwitch # Get VM and Adapter Info **$myvmname = Get-VM -Name $vmName** **$myNetworkAdapters = $vmname | Get-NetworkAdapter** # Initialize data array $data = @() # Loop through all distributed switches foreach ($dSwitch in $distSwitches) { $networks = $dSwitch | Get-VDPortgroup foreach ($network in $networks) { # If network name contains "CORP" and VLAN ID is 100 if ($network.VLanId -eq 100 -and $network.Name -like "*CORP*") { $data += New-Object PSObject -Property @{ "vCenter" = $vcServer "Host" = $ESXHost "VMName" = $vm.Name "SwitchName" = $dSwitch.Name "NetworkName" = $network.Name "VlanId" = $network.VLanId "SwitchType" = "Distributed" } } } } # Loop through all standard switches foreach ($sSwitch in $standSwitches) { $networks = $sSwitch | Get-VirtualPortGroup foreach ($network in $networks) { # If network name contains "CORP" and VLAN ID is 100 if ($network.VLanId -eq 100 -and $network.Name -like "*CORP*") { $data += New-Object PSObject -Property @{ "vCenter" = $vcServer "Host" = $ESXHost "VMName" = $vm.Name "SwitchName" = $dSwitch.Name "NetworkName" = $network.Name "VlanId" = $network.VLanId "SwitchType" = "Standard" } } } } # Export data to CSV $data | Export-Csv -Path $exportPath -NoTypeInformation # Disconnect from vCenter Disconnect-VIServer -Server $vcServer -Confirm:$false
The reason why I require this information is so that I can report on the virtual machines that are configured with these VLAN's and export it out on a daily basis. Then I'll do a comparison of the latest and previous file to see if there has been any recent changes, this is notified within a changes.csv file with a Side Indicator => or <=.
Hope this helps understand the reasoning behind this requirement.
Unless you only have 1 VM on each Portgroup, there is no 1-to-1 relation between Portgroups and VMs.
You can list all the VMs connected to a portgroup, for VSS and VDS portgroups, with
"VMName" = (Get-VM -RelatedObject $network).Name -join ','
Not sure how you want to use the MAC and IP of each network adaptor of a VM connected to a portgroup to allow you to search for changes. Again, this is not a 1-to-1 relation.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference