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.

Populate-vDS-portgroup.ps1

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

	Write-Host "Now connected to " -ForegroundColor Magenta
	$global:DefaultVIServer.name	

	# $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
	$global:DefaultVIServer.name
}

Write-Host
 
# 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
 
Write-Host
 
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 :

.Populate-vDS-Portgroups.ps1
Currently connected to
xxx.xxx.xxx.xxx

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. Write my essay

    Saturday, October 28, 2017 - 13:40:27

    This content is very important and useful to wine and spirits points followers. They have to get best buy status with the following methods. That everything getting to find more winery articles and reviews.

  2. online assignment writing service

    Saturday, November 25, 2017 - 07:20:26

    Pleasant to visit your site page once more, it has been months for me. Well this article i have been sat tight for so help with essay I require this piece to finish my task in school, and it has same theme with your piece.

  3. i need a dissertation

    Wednesday, November 29, 2017 - 13:20:25

    Wow! I am really impressed by the way you detailed out everything. It is really going to help me a lot. Thanks for sharing your thoughts so clearly.

  4. pay to do homework

    Tuesday, December 12, 2017 - 08:03:02

    On the off chance that effectively associated, you’ll bear on as ordinary, if not associated, you’ll be provoked for the VC and the content will detach you toward the end.

  5. Do my Assignment Cheap UK

    Thursday, December 14, 2017 - 08:10:17

    PowerCLI as “an intense summon line tool that gives you a chance to robotize all parts of vSphere management, including network, stockpiling, VM, visitor OS and more.

  6. Do my Assignment

    Thursday, December 21, 2017 - 13:49:09

    I adore seeing online journal that comprehend the benefit of giving a quality data to free. Exceptionally decent offer and continue sharing. I can see that you are a specialist at your field! I am propelling a site soon, and your data will be extremely helpful for me.

  7. TimothyPes

    Sunday, January 14, 2018 - 06:16:40

    Earn on the Internet from $ 5000 per day, do not strain, the details are here: http://pagebin.com/CGiNOrkf

    http://casinoonline com, how to make money on the internet in south africa, casino royale online sa prevodom filmovizija, make money online daily payment, make extra money from home 2015, how to make a little extra cash on the side, how much do you make selling avon products, earn money through quizzes
    , jobs to earn extra money part time, best affiliate money making program, genuine online money earning in india, how do i make a wire transfer payment, earn extra money working from home south africa, way to earn money online, online casino no deposit bonus no download, how mothers can make money at home, creative ways to make money from home, paypal instant transfer vs echeck, casino royale full movie Щ…ШЄШ±Ш¬Щ…
    , how to make money online magazine, free online casino games in india, how make money blogging online, tropicana atlantic city online casino, easy ways to make money from home in india, i want to make money, caesar casino online review, how to make cash while at home

Add comment

Fill out the form below to add your own comments