VMware Cloud Community
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Modifying script to automate Standard vSwitch VM Port Group setting creation ?

People,

I'm deploying multiple ESXi servers which needs to have access to lots of VLAN, hence I need to create about 10-15 VLANs as VM Port Group (Network Label) in one single ESXi host and as standard vSwitch.

This ESXi hosts does not have the capability of Distributed Switch, hence I must manually create the VLAN/Netowrk label one by one in standard vSwitch, and then move on to another ESXi hosts in all of the racks in the data center.

So I wonder if anyone here please can assist me in how to modify the script below to automate the VLAN creation in vSwitch2 from VLAN 1 up to 20 ?

Get-VirtualSwitch -Name vSwitch2 -VMHost PRODESXi01 |

   New-VirtualPortgroup -Name "Company_Network_VLAN $($_.VLAN)" -VLANID $($_.VLAN)

Existing:

Name of the vSwitch: vSwitch2

VM Port Group Label: Company_Network_VLAN1

VLAN ID: 1

VM Port Group Label: Company_Network_VLAN2

VLAN ID: 2

....

....

VM Port Group Label: Company_Network_VLAN20

VLAN ID: 20

Thanks in advance.

/* Please feel free to provide any comments or input you may have. */
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Something like this ?

foreach($esx in Get-VMHost){

  $sw = Get-VirtualSwitch -Name vSwitch2 -VMHost $esx

  1..20 | %{

    New-VirtualPortgroup -VirtualSwitch $sw -Name "Company_Network_VLAN $($_)" -VLANID $_

  }

}


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

Something like this ?

foreach($esx in Get-VMHost){

  $sw = Get-VirtualSwitch -Name vSwitch2 -VMHost $esx

  1..20 | %{

    New-VirtualPortgroup -VirtualSwitch $sw -Name "Company_Network_VLAN $($_)" -VLANID $_

  }

}


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

AlbertWT
Virtuoso
Virtuoso
Jump to solution

LucD‌, if I have some VLAN number to be created that is non-sequential like 2,6,7,8,51,110,165,166,167,168,2200,3500,4000

Do I just modify the script:

foreach($esx in Get-VMHost){

  $sw = Get-VirtualSwitch -Name vSwitch2 -VMHost $esx

  2,6,7,8,51,110,165,166,167,168,2200,3500,4000 | %{

    New-VirtualPortgroup -VirtualSwitch $sw -Name "Company_Network_VLAN $($_)" -VLANID $_

  }

}

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that should work.


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

AlbertWT
Virtuoso
Virtuoso
Jump to solution

LucD‌, yes it works 🙂

You are so amazing.

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos