Monday, May 25, 2015

Mass creation of vDS portgroups using PowerCLI

There’s a potential project going on to migrate off of the Nexus 1000v distributed switch. One of the things that will be needed, is to create portgroups for all the existing Nexus port profiles.

Following is a simple PowerCLI script put together to help with this. It simply creates the port groups from entries in a .csv file. Nothing else is done with respect to the migration.

It’s a practical, functional script. It’s not fully featured or doing anything clever. In essence, it’s what I tend to use Powershell and PowerCLI for, getting tedious things done quickly. It’s missing the “correct” way to do things, but took 10 minutes to put together - which shows.

The core of the script is basically a couple of lines. Fleshed out with some comments, and an initial check on whether a connection has been made to the vCenter before running the script. Typically in my everyday work, I have PowerCLI connections made to the VC’s, so don’t like scripts to nag me to connect. But I’ve added a rudimentary check for this. If already connected, you’ll carry on as normal, if not connected, you’ll be prompted for the VC and the script will disconnect you at the end.

The other assumption here is that all the portgroups are going to be of type VLAN - ie, we’ve got trunked NICs, so each portgroup will need a VLAN ID set.

The script uses a .csv file that you will need to generate - this is my typical approach on these things, as I just find it easy to work with. The layout of the .csv file (ie the column name that you will need) is :

vDS - name of vDS that we want to create the portgroup on
pgName - name of the portgroup to create
NumPorts - number of ports for the portgroup
vlanID - vlanID.

Once it’s created the portgroups, it will just print out the name, number of ports, port binding and vlan configuration for every portgroup on the dVS for confirmation.


# Check if already connected to VC or not. If not, prompt to.
if ($ -eq $null) {
	$vc = Read-Host "Enter the VC to connect to"
	Connect-VIServer $vc

	Write-Host "Now connected to " -ForegroundColor Magenta

	# $wasConnected set to no - this will be used at the end to prompt 
	# whether to disconnect the session.
	$wasConnected = "no"
else {
	# There was already a session connected - display it and continue.
	Write-Host "Currently connected to " -ForegroundColor Magenta

# Read in the .csv file with all the details that we'll need
# Layout (ie, column names) of the .csv is  :
# vDS - name of vDS that we want to create the portgroup on
# pgName - name of the portgroup to create
# NumPorts - number of ports for the portgroup
# vlanID - vlanID - assumption here is we've got a trunk, and each portgroup needs 
# it's own vlan ID to identify the tag
$srcFile = Read-Host "Enter the name of the .csv file to work with"
$vdsPortgroup = Import-Csv $srcFile
foreach ($portgroup in $vdsPortgroup){
	Get-VDSwitch $portgroup.vDS | New-VDPortgroup -name $portgroup.pgName -NumPorts $portgroup.numports -VlanId $portgroup.vlanID

Write-Host "`nPortgroups created. Now confirming settings" -ForegroundColor Cyan

Get-VDSwitch $portgroup.vDS | Get-VDPortgroup | select name, numports, portbinding, vlanconfiguration
# If a connection was needed to the VC at the start of the script, then prompt
# to confirm whether to disconnect the session.
if ($wasConnected -eq "no")
	Disconnect-VIServer $vc -confirm:$true

Sample output :

Currently connected to

Enter the name of the .csv file to work with: dvs-test.csv

Name NumPorts PortBinding
—- ——– ———–
scriptpg1 100 Static
scriptpg2 150 Static
scriptpg3 100 Static
scriptpg4 200 Static
scriptpg5 125 Static

Portgroups created. Now confirming settings

Name : scriptpg1
NumPorts : 100
PortBinding : Static
VlanConfiguration : VLAN 500

Name : scriptpg5
NumPorts : 125
PortBinding : Static
VlanConfiguration : VLAN 504

Name : scriptpg2
NumPorts : 150
PortBinding : Static
VlanConfiguration : VLAN 501

Name : scriptpg4
NumPorts : 200
PortBinding : Static
VlanConfiguration : VLAN 503

Name : scriptpg3
NumPorts : 100
PortBinding : Static
VlanConfiguration : VLAN 502

Name : dvPortGroup
NumPorts : 128
PortBinding : Static
VlanConfiguration :

Name : CloudMgmt-vDS-DVUplinks-888
NumPorts : 2
PortBinding : Static
VlanConfiguration : VLAN Trunk [0-4094]

  1. buy essays

    Monday, February 19, 2018 - 12:35:44

    I like this post. I want to more information about this topic and now, I got all information from your post. Everything defines in this post is awesome. I think you should make more post which helps us.I also share a resource relevant your topic. You can compare with my blog

  2. Do My Essay For Me

    Monday, May 7, 2018 - 12:44:58

    When you make another portgroup, it naturally incorporates the dvUplinks, however for this situation, we needed to evacuate them and utilize the LAG gathering. We likewise physically made the switch and the LAG assemble before running this switch.

  3. gunship battle mod

    Friday, September 21, 2018 - 14:09:25

    Awesome post about also check it Blog!

  4. temple run 2 mod

    Friday, September 21, 2018 - 14:10:17

    Thanks a lot for sharing this useful information.

  5. mobile legends mod

    Friday, September 21, 2018 - 14:11:10

    Thank You For Amazing Information Admin Please Check My Article On

  6. Make My Audit Assignment

    Saturday, October 6, 2018 - 15:32:54

    This Is Really A Great Stuff For Sharing. Keep It Up .Thanks For Sharing.

  7. australia essay writing

    Saturday, October 20, 2018 - 03:30:03

    There is too many information. I can handle that on my own.

  8. Myles

    Wednesday, October 31, 2018 - 15:18:05

    I’ve been browsing online more than 3 hours today, yet I never
    found any interesting article like yours. It is pretty worth enough for me.

    In my view, if all web owners and bloggers made good content as you
    did, the internet will be a lot more useful than ever before.


    Monday, December 17, 2018 - 21:02:19

    It’s genuinely very complex in this active life to listen news on TV, thus I only use world
    wide web for that purpose, and obtain the most recent information.

  10. Dissertation writing services

    Monday, December 31, 2018 - 12:50:36

    I would like to appreciate all your efforts, I have used many other services several times and I am very happy with it too.

  11. Hero Instinct

    Sunday, January 6, 2019 - 13:10:18

    Three are usually cheap Ralph Lauren available for sale each and every time you wish to buy.

  12. Text Chemistry

    Sunday, January 6, 2019 - 14:24:34

    This is also a very good post which I really enjoy reading. It is not everyday that I have the possibility to see something like this.

  13. Relationship Rewrite Method

    Sunday, January 6, 2019 - 14:33:55

    Excellent effort to make this blog more wonderful and attractive.

  14. 13 Zodiac Signs

    Sunday, January 6, 2019 - 14:42:40

    This is really very nice post you shared, i like the post, thanks for sharing..

  15. How To Make Yourself Pee

    Sunday, January 6, 2019 - 16:09:19

    What a really awesome post this is. Truly, one of the best posts I’ve ever witnessed to see in my whole life. Wow, just keep it up.

  16. How To Turn a Guy On

    Sunday, January 6, 2019 - 16:15:37

    Hey, great blog, but I don’t understand how to add your site in my rss reader. Can you Help me please?

  17. How I Work

    Thursday, January 17, 2019 - 16:44:04

    I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.

  18. Buy Essay Online

    Monday, February 11, 2019 - 09:45:05

    Do you believe in magic? Let’s try a trick! Ask Buy Online Essay, “Can you write my essay for me” and place an order. Our writers will work behind the scenes to fulfil your fantasy. The result will be revealed to you on your selected date of submission. Order now!

  19. cialis

    Thursday, May 9, 2019 - 05:12:06

    Hmm it appears like your blog ate my first comment (it
    was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your
    blog. I as well am an aspiring blog blogger but I’m still new to the whole thing.
    Do you have any suggestions for rookie blog writers?
    I’d certainly appreciate it.

  20. Perez

    Tuesday, June 4, 2019 - 04:39:58 Bottega Veneta Christian Louboutin Heels Ray Ban Sunglasses Christian Louboutin Nike Roshe Adidas Sneakers Fitflop Sale Clearance Jordan Shoes Oakley Standard Issue Fitflop Sandals Clearance Oakley Eyeglasses Oakley Si ECCO Shoes Christian Louboutin Shoes Adidas Outlet Air Max Oakley Sunglasses Ray Ban Wayfarer Sunglasses Cheap Oakley Sunglasses Pandora Goyard Handbags Pandora UK Christian Louboutin Shoes Fitflop Outlet Pandora Bracelet Pandora Jewelry Nike Outlet Adidas Superstar Jordan 11 Low Fitflop Sandals Longchamp Outlet Louboutin UK Bottega Veneta Handbags Pandora Jewelry Pandora Charms Pandora Jewelry Official Site Oakley Prescription Sunglasses Ferragamo Outlet Pandora Pandora Jewelry NMD R1 Pandora Jewelry Pandora Jewelry Official Site Black Friday Oakley Sunglasses Ralph Lauren Balenciaga ECCO Ray Ban Sunglasses Outlet Nike Outlet Store Ray Ban Sunglasses On Sale Moncler Jackets Pandora Charms Adidas NMD Fitflop Sale Clearance Adidas Yeezy Ed Hardy Yeezy Balenciaga Sneakers Ray Ban Eyeglasses Pandora Official Site Birkenstock Sandals On Sale Moncler Outlet Air Max Nike Factory Outlet Christian Louboutin Valentino Outlet Ray Ban Sunglasses Oakley Sunglasses Ray Bans Moncler Outlet Store Yeezy Pandora Ray Ban Aviator Sunglasses Nike Outlet Store Goyard Bags Pandora Charms Jewelry Pandora Charms Canada Red Bottom Shoes Pandora Outlet Birkenstock Outlet Online Cheap Ray Ban Sunglasses Pandora Charms Outlet Moncler Official Site Pandora Bracelet Charms Adidas Yeezy Ray Ban Sunglasses Moncler UK Ferragamo Shoes Nike Outlet Kate Spade Skechers Adidas Yeezy Kate Spade Purses Nike Sneakers Balenciaga Sneakers Jordan 11 Birkenstock Sandals Oakley Outlet Pandora Jewelry Timberland Boots

Add comment

Fill out the form below to add your own comments