Get Unused PortGroups in a Cluster
Hi All!
I've seen many blogs and discussions about this proposite so i put my hands on the problem and generate my own script.
The algortihm is easy to understand.. You get a list of the vPorts used by the Vms and another list from the vPorts of the Cluster. Then i do something just like a procv in Excel and compare both lists, keeping the difference in another list, the unused PortGroup List.
To use, you have to put the Vcenter which you want to connect, choose the Cluster and wait a few seconds, then you have a csv Document in your c:\temp directory. ![]()
Any sugestions i'll be here to update the script. Thanks!
Thanks to LucD by inspiring me on it!
Now, the output is OK, with no duplicated items. ![]()
Clear
Write-Output " "
$Vcenter = Read-Host "Which Vcenter do you want to connect?"
Write-Output " "
Connect-Viserver -server $VCenter -WarningAction SilentlyContinue
# Getting Cluster info
$Cluster = Get-Cluster
$countCL = 0
Clear
Write-Output " "
Write-Output "Connected to $VCenter successfully!"
Write-Output " "
Write-Output "Clusters: "
Write-Output " "
foreach($oC in $Cluster){
Write-Output "[$countCL] $oc"
$countCL = $countCL+1
}
$choice = Read-Host "Which Cluster do you want to verify?"
Write-Output " "
Write-Output "Wait a minute..."
$cluster = get-cluster $cluster[$choice]
$vms = $cluster| get-vm
$Data = @()
foreach ($VM in $VMs){
$NICs = $VM.NetworkAdapters | select networkName
foreach ($NIC in $NICs) {
$into = New-Object PSObject
Add-Member -InputObject $into -MemberType NoteProperty -Name VMname $VM.Name
foreach($n in $NIC){
Add-Member -InputObject $into -MemberType NoteProperty -Name NetworkName $n.NetworkName }
$Data += $into
}
}
##
##$Data contains the list of virtual machines/Networkadapters by cluster
##$ClustervPortsData contains a list of the vPorts by Cluster
##
$ClustervPortsData = $cluster |get-vmhost | Get-VirtualPortGroup | select @{n="Cluster";e={$cluster.name}},Name
##
##Comparing vPorts used by VMs in that Cluster and the CLuster vPorts, we'll have the vPorts Unused...
##$Data x $ClustervPortsData
$a = $data | select NetworkName
$b = $ClustervPortsData | select Name
$cont = 0
$finalData = @()foreach($b1 in $b){
$obj = New-Object PSObject
foreach($a1 in $a){
if($b1.Name -eq $a1.NetworkName){
$cont = $cont+1
}elseif($b1.Name -ne $a1.NetworkName){
}
}
if($cont -eq 0){
#Add-Member -InputObject $obj -MemberType NoteProperty -Name VPORT $b1.Name
$obj = $b1.Name
$finalData += $obj
}
$cont = 0
}
$rand = Get-Random
$cluname = $Cluster.name
$CL = "$cluname$rand"
$path = "c:\temp\vPortsNotUsed_$Vcenter$CL.csv"
############################
##function to remove duplicated items
Function Remove-DuplicatedItems{
param ($arraylist)
$RemoveDup =@()$arraylist|%{
$itemDup = $_
#compare if the $itemdup already exists in the list
if($removedup -contains $itemDup){
#do nothing
#Write-host "$itemdup already exists in the new array"
}else{
#add to the new array unduplicated
#Write-host "$itemdup do not exists in the new array, adding"
$RemoveDup += $itemdup
}
}
return $RemoveDup
}
############################Remove-DuplicatedItems $finaldata | export-csv $path -NoTypeInformation -UseCulture
clear
Write-Output " "
Write-Output "The File $path was created and it's path was copied to the clipoard. Press any button to exit..."
Read-Host " "
$path | clip
Write-Output " "
Write-Output "Disconnecting from Vcenter $Vcenter..."
Write-Output " "
Disconnect-viserver * -Confirm:$false