VMware Cloud Community
brianpfrank
Contributor
Contributor

Can we speed up this port group sync script?

We've built this script grabbing bits and pieces from other scripts to  suit our needs.  Problem is with 50+ PGs on each host it takes over an hour to run.  Is there any way to speed it up?  I tried to figure out how to add the -RunAsync but with the foreach loop I don't think that will not work.

################################################################################################
# This script will remove all port groups from the $DestHost excluding any port groups         #
# that have these names. Management Network, or VMotion. Once done the the script scans all    #

# port groups on the $SourceHost and starts applying those one by one to the $DestHost.        #

################################################################################################

Connect-VIServer vcenter.yourdomain.com

#Remove
$DestHost = "desthost.yourdomain.com"
$SourceHost = "srchost.yourdomain.com"
$pgfiltered = Get-VirtualPortGroup -vmhost $DestHost  | where {$_.name -notmatch "Management Network" -and $_.name -notmatch "VMotion"}
foreach ($_ in $pgfiltered)
{
remove-Virtualportgroup $_ -Confirm:$false
}

#Put back
$newvs = Get-VirtualSwitch -VMHost $DestHost
$pgcorrect = Get-VirtualPortGroup -VMHost $SourceHost | where {$_.name -notmatch "Management Network" -and $_.name -notmatch "VMotion"}
foreach ($_ in $pgcorrect)
{
New-VirtualPortGroup -VirtualSwitch $newvs -Name $_.name -VLanId  $_.vlanid
}

Thanks!!!  For your help!

Brian

0 Kudos
2 Replies
lockenkeyster
Enthusiast
Enthusiast

I'm not sure how much using RunAsync will speed things up here because the tasks will get queued up on the vCenter Server anyway. The script will end sooner, but it seems that the tasks will still take roughly the same amount of time to complete.

Is there any overlap between the lists of VM portgroups on the destination host and those on the source host? My only thought is to work off of a diff of the two lists. So, instead of unnecessarily removing portgroups that are readded again later, you just don't remove them in the first place.

0 Kudos
admin
Immortal
Immortal

Hi, there,

I have an idea, about the removal. You are passing 1 by 1 the portgroups to the

Remove-VirtualPortGroup cmdlet, but actually it can process a whole array of portgroup objects. See here

That way if you pass them, the cmdlet could try to process them in batch and speed up a little.

Other than this, I think  lockenkeyster is right - if it all queues at the VCenter, there's nothing PowerCLI can do about it.

Best regards,

Leni Kirilov

PowerCLI team

0 Kudos