VMware Cloud Community
BorisBorshevsky
Contributor
Contributor
Jump to solution

copy portgroups script

Hello,

I’m looking for a script that can copy all the Vlans (the portgroups) on certain vSwitch (vSwitch1 in most cases)

Then add them identically to a newly installed ESX (group of ESXs if possible) to the same vSwitch

Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

A simple portgroup replication script.

Note that this does not copy the teaming policy !

If that is needed the script would need to use the Get-NicTeamingPolicy and Set-NicTeamingPolicy cmdlets.

$modelEsxName = "esx1" 
$modelSwitchName
= "vSwitch1"
$tgtEsxNames = "esx2","esx3","esx4"

$esx
= Get-VMHost -Name $modelEsxName
$sw = Get-VirtualSwitch -Name $modelSwitchName -VMHost $esx
$pg = Get-VirtualPortGroup -VirtualSwitch $sw
foreach($newEsx in (Get-VMHost -Name $tgtEsxNames)){
   
$newSw = Get-VirtualSwitch -Name vSwitch2 -VMHost $newEsx
    $pg | %{
       
$newPg = New-VirtualPortGroup -Name $_.Name -VLanId $_.VLanId -VirtualSwitch $newSw -Confirm:$false
    }
}
    


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
ChrisDearden
Expert
Expert
Jump to solution

Alan Renoufs vProfiles script is the best one I know.

http://www.virtu-al.net/2009/06/29/powercli-vprofiles/

If this post has been useful , please consider awarding points. @chrisdearden http://jfvi.co.uk http://vsoup.net
LucD
Leadership
Leadership
Jump to solution

A simple portgroup replication script.

Note that this does not copy the teaming policy !

If that is needed the script would need to use the Get-NicTeamingPolicy and Set-NicTeamingPolicy cmdlets.

$modelEsxName = "esx1" 
$modelSwitchName
= "vSwitch1"
$tgtEsxNames = "esx2","esx3","esx4"

$esx
= Get-VMHost -Name $modelEsxName
$sw = Get-VirtualSwitch -Name $modelSwitchName -VMHost $esx
$pg = Get-VirtualPortGroup -VirtualSwitch $sw
foreach($newEsx in (Get-VMHost -Name $tgtEsxNames)){
   
$newSw = Get-VirtualSwitch -Name vSwitch2 -VMHost $newEsx
    $pg | %{
       
$newPg = New-VirtualPortGroup -Name $_.Name -VLanId $_.VLanId -VirtualSwitch $newSw -Confirm:$false
    }
}
    


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BorisBorshevsky
Contributor
Contributor
Jump to solution

Many thanks, I will try this.

But I’m looking for a bit shorter script that only copies the portgroups since ll the other things are already scripted;

The portgroup copy is done by ESX command; (printing all portgroups into file using awk than copy the file and add them)

Seems that doing it with powerCLI will be way faster.

0 Kudos