5 Replies Latest reply on Feb 15, 2013 11:22 AM by TheVMinator

    Creating port groups on a vSphere Distributed Switch with PowerCLI

    TheVMinator Master

      I have a large number of new port groups that I want to create on an existing vSphere Distributed Switch.  They will mostly have the same settings with only a few differences.   How can I automate this with PowerCLI?  I'd like to specify the name of an existing distributed switch, then have the script list the port groups that I'm going to add.

      Thanks!

        • 1. Re: Creating port groups on a vSphere Distributed Switch with PowerCLI
          RvdNieuwendijk Virtuoso
          vExpertUser Moderators

          You can use the distributed switch cmdlets new to PowerCLI 5.1 Release 2:

           

          New-DistributedSwitchPortGroup -Name vDS01-VLAN22 -DistributedSwitch vDS01 -VLAN 22

          • 2. Re: Creating port groups on a vSphere Distributed Switch with PowerCLI
            LucD Guru
            User ModeratorsvExpertCommunity Warriors

            How will you be providing the portgroup names ? In a CSV file ?

            If yes, you could do something like

            $vds = Get-VDSwitch -Name MyVDS

            Import-Csv C:\portgroup-names.csv | %{

               New-VDPortgroup -VDSwitch $vds -Name $_.Name -NumPorts 32

            }

            This assumes that your CSV looks like

            Name

            pg1

            pg2

            pg3

            2 people found this helpful
            • 3. Re: Creating port groups on a vSphere Distributed Switch with PowerCLI
              TheVMinator Master

              Actually, I probably won't have enough to require a CSV file, but I do need to specify the following 5 fields for each port group:

               

              -Name of Port Group
              -Description of Port Group
              -names of active uplinks
              -Number of ports to use for the port group
              -vlan ID to assign to the port group

               

              I could use a CSV file, but how would I specifiy those 5 attributes for each port group?

               

              (Thanks again Luc for your input, and Robert)

              • 4. Re: Creating port groups on a vSphere Distributed Switch with PowerCLI
                LucD Guru
                Community WarriorsvExpertUser Moderators

                You can have multiple columns in a CSV, so that is not really a problem.

                If you make your CSV something like this

                Name,UplinkPorts,Vlan

                pg1,32,12

                pg2,64,14

                pg3,64,2001

                You can pass those values in the script like this

                $vds = Get-VDSwitch -Name MyVDS

                Import-Csv C:\portgroup-names.csv | %{

                   New-VDPortgroup -VDSwitch $vds -Name $_.Name -NumPorts $_.UplinkPorts -VlanId $_.Vlan

                }

                For the other 2 properties, description and uplinknames, you have to set these in another way.

                For these you will need to use one of the SDK methods.

                In fact the UplinkNames are set on the dvSwitch, not on the dvPortgroup afaik. Unless you mean something else ?

                 

                I'll get back to you for those 2 missing properties.

                1 person found this helpful
                • 5. Re: Creating port groups on a vSphere Distributed Switch with PowerCLI
                  TheVMinator Master

                  Here is a better explanation of what I’m thinking.  For example, lets say I want to have 5 fields in my csv file, in the following order, that create a port group called “mgt-pg1”.

                  1. Name = Name of Port Group  (in this example, the name should be “mgt-pg1”)
                  2. Description = Description of Port Group  ( I want the description field in the properties sheet for the port group to read “Management Port Group Number 1”
                  3. ActiveUplinkNames= Identifies which uplinks (which will already have been defined in the vDs) will be identified as “Active” uplinks for this port group.  For example, I want to use 2 uplinks, already defined in the vds, called “dvuplink1” and “dvuplink2”.  I want both links to be active.  I don’t need any standby uplinks.
                  4. NumPorts =Number of Ports to use for the Port Group  ( I want to have 64 ports on this port group)
                  5. VlanID = VLAN ID (my vlan ID is 220)

                   

                  So I want a CSV file that has a line for this port group, something like:

                   

                   

                  Mgt-pg1, “Management Port Group Number 1”, {dvUplink1,dvUplink2}, 64, 220

                   

                   

                  Then I want to be able to use these 5 settings to define my port group using PowerCLI.

                  Thanks again