- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Here’s my contribution... for a mass delete of portgroups on virtual standard switch
first a script (heavily plagiarized from Guilherme) which double checks that no ports are in use
It feeds from a txt file, C:\vSpherePowerCLI\input\unusedportgroups.txt
Note: be careful there is no whitespace, additional lines at the end of the txt..
After that is the delete portgroups, a bit crude, and slow, but we have cleaned up 400+ port groups so far
Its taking around 120 seconds for a 7 node cluster, so several hours if you are removing 100s of unused portgroups
checkUnusedPortGroupsInCluster
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| ## Double check the imput of C:\vSpherePowerCLI\input\unusedportgroups.txt before deleting mas |
# Enter location
Clear
| # Getting Cluster info | |
| $Cluster = Get-Cluster | |
| $countCL = 0 | |
| Clear | |
| 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] |
# list the vms in the cluster, create the placeholder variable @Data
| $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 input from txt | ||
| $ClustervPortsData = Get-Content "C:\vSpherePowerCLI\input\unusedportgroups.txt" | ||
| ##Comparing vPorts used by VMs in that Cluster and the CLuster vPorts, we'll have the vPorts Unused... | ||
| ##$Data compared to $ClustervPortsData | ||
| $vmpg = $data | select NetworkName | ||
| $esxpg = $ClustervPortsData | ||
| $cont = 0 | ||
| $finalData = @() | ||
| foreach($esxpg1 in $esxpg) |
{
$obj = New-Object PSObject
## here the vm portgroup is compared with the esx portgroup, if it is in use then $cont is incremented
foreach($vmpg1 in $vmpg)
{
if($esxpg1 -ne $vmpg1.NetworkName)
{
$cont = $cont+1
}
if($esxpg1 -eq $vmpg1.NetworkName)
{
Write-Host "ERROR: Virtual Machine:" "Adapter:" ""$esxpg1"" "connected" -foregroundcolor red
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
if($cont -eq 0)
{
Write-Host "ok ..."
}
$cont = 0
}
| clear | |
| Write-Output "Finished" | |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
deleteMultipleUnusedPortGroupsInCluster
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Delete multiple unused portgroups from cluster
## Only use after adding the portgroups from GetUnusedPortGroupsInCluster.ps1
## Double check with CheckUnusedPortGroupsInCluster.ps1
# set foreground color to yellow
[console]::ForegroundColor = "yellow"
Write-Output " "
Write-Output "This script will delete multiple portgroups "
Write-Output "add the portgroup names to C:\vSpherePowerCLI\input\unusedportgroups.txt "
Write-Output " "
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Clear
# Getting Cluster info
$Cluster = Get-Cluster
$countCL = 0
Clear
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 delete the portgroups on?"
Write-Output " "
Write-Output "This is going to take a while..."
$cluster = get-cluster $cluster[$choice]
# manually enter the location where you want to remove the portgroup
#$Location = Read-Host "Enter location; cluster or datacenter"
# reset the colors back to default
[console]::ResetColor()
# Set system variable to ignore error messages
$ErrorActionPreference= 'silentlycontinue'
### Input from txt the portgroup name you want to remove
$OldPortGroupNames = Get-Content "C:\vSpherePowerCLI\input\unusedportgroups.txt"
foreach ($OldPortGroupName In $OldPortGroupNames)
{
foreach ($esx in get-VMhost -Location $cluster | sort Name)
{
Get-VMhost $esx | Get-VirtualPortGroup -name "$OldPortGroupName" | Remove-VirtualPortGroup -Confirm:$false
}
}