Hi @LucD ,
Hope you are doing well. I need small help from you .
I am trying to get list of vm's inside specific vlan but for some reason csv shows repetetive output.For exapmle on console output i see vlan 123 got vmabc but in csv i get same entry for vmabc twice/thrice.
Following is my code i am trying to run. All i am after is list of vms (even if not connected to n/w but part of vlan123) in the csv output just once.
Could this be because you are Appending to the CSV without initialising it at the start of the script?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here is another script for almost or maybe the same purpose as yours, though it is slightly easier to track/troubleshoot.
The migrationGroup variable is commented out. Uncomment it (on both locations) in order to have it in the same CSV report.
# Define the variables here
$vCenter = "vcenter0.local","vcenter1.local","vcenter2.local"
$VLAN = "123","321"
$ReportExportCSVFile = "C:\report.csv"
$vCenterUser = "user1"
$vCenterPassword = "Password123"
#$migrationgroup = "Group9"
# Set the powercli configuration DefaultVIServer to Multiple, so that you can work with all vCenters at once
Set-PowerCLIConfiguration -Scope Session -DefaultVIServerMode Multiple -Confirm:$false
# Connect to all vCenters. If it can't, stop the command.
try{
Connect-VIServer -Server $vCenter -User $vCenterUser -Password $vCenterPassword
}
catch{
break
}
# Function that will find all VMs on a specific vlan
function Get-VMsForVLAN {
param([int]$vlanID)
# Get all distributed port groups accross all vCenters
$VDPortGroups = Get-VDPortgroup | Where { $_.VlanConfiguration.VlanId -eq $vlanID}
$networks = @()
foreach($vlanName in $VDPortGroups){
$networks += Get-View -ViewType Network -Property Name -Filter @{"Name" = $($vlanName.name)}
}
$networks | ForEach-Object {
($_.UpdateViewData("Vm.Name", "Vm.Runtime.PowerState", "Vm.Guest.IPAddress"))
}
# Create an empty array
$VMsForVLanResult = @()
foreach($VDPG in $networks){
# Find the vCenter where that PG resides.
$PGvCenter = (($VDPG.Client.ServiceUrl -split "//")[1] -split "/")[0]
# Get all VMs that use that PG in that particular vCenter
if($VDPG.LinkedView.Vm.Name){
$VMsInPG = $VDPG.LinkedView.Vm
}
else{
continue
}
foreach($VMPG in $VMsInPG){
# Build a hash table with all the necessary information
$props = [ordered]@{}
$props.'vCenter' = $PGvCenter
$props.'VM' = $VMPG.Name
$props.'PowerState' = $VMPG.Runtime.PowerState
#$props.'MigrationGroup' = $migrationgroup
$props.'PortGroup' = $VDPG.Name
$props.'VLAN' = $vlanID
$props.'IP' = $VmPG.Guest.IPAddress
# Store it in the Array defined above
$VMsForVLanResult += New-Object -TypeName PScustomObject -Property $props
}
}
# When all VPGs are processed, return the result.
return $VMsForVLanResult
}
# Store the information into a Variable
$VLANResult = $VLAN | ForEach-Object {Get-VMsForVLAN -vlanID $_}
# Print the result in the console
$VLANResult | ft -AutoSize
# Export the result to a CSV.
$VLANResult | Export-Csv -NoTypeInformation -Path $ReportExportCSVFile
# Disconnect from all vCenters
Disconnect-VIServer -Server * -Force -Confirm:$false
but i am initializing it
$connectedVMCsv = New-Object System.Collections.ArrayList
$migrationCsv = New-Object System.Collections.ArrayList
or i missed something
What do you mean with "broken state"? Which part of the script shows you the VMs that are in broken state? Can you elaborate a little bit?
what is broken state?
