VMware Cloud Community
Gman999
Contributor
Contributor

Script to tag Distributed Port Groups

Hello I need help with a script to tag all Distributed Port Groups in our Vcenter. I can pull RVtools report and use the information as csv to asist in what needs to be tagged?

 

 

0 Kudos
8 Replies
LucD
Leadership
Leadership

Can you be a bit more specific?
Which portgroups, is there a selection condition?
What tag do you want to assign? An existing one, or a new one.


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

0 Kudos
Gman999
Contributor
Contributor

Hello @LucD 

Thank you for assisting, We are deploying VRA into the environment and we need all the portgroups that we have to be tagged.

In one vcenter we have 192 portgroups. Each port group is assigned to a VLAN. We have created tags already for each VLAN for example Assign Tag = 1812 and Category = VLAN_Tag so this tag needs to be assigned to Portgroup 1812.

And the same needs to happen for the rest of the portgroups

0 Kudos
LucD
Leadership
Leadership

You could do something like this.
Note that the script currently only handles single VLANs, no trunks or PVLANs

Get-VDPortgroup -PipelineVariable pg | where{$_.VlanConfiguration -is [VMware.VimAutomation.Vds.Types.V1.SingleVlanConfiguration]} |
ForEach-Object -Process {
  try{
    $tag = Get-Tag -Name $pg.VlanConfiguration.VlanId -ErrorAction Stop
  }
  catch {
    $tag = New-Tag -Name $pg.VlanConfiguration.VlanId -Category 'VLAN_Tag'
  }
  New-TagAssignment -Entity $pg -Tag $tag
}


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

0 Kudos
Gman999
Contributor
Contributor

Hello @LucD 

Thank you, I want to test it on one vds for first. What can I add that it only connect to a specific vds I want to run it against?

Tags (1)
0 Kudos
LucD
Leadership
Leadership

Change the start

Get-VDSwitch -Name <MyvdSw> | Get-VDPortgroup -Name <MyvdPg> -PipelineVariable pg


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

0 Kudos
Gman999
Contributor
Contributor

Wow thanks for the quick reply

0 Kudos
Gman999
Contributor
Contributor

Hello @LucD

I have run the below and getting output below, I just removed the actual vcenter and Dvs and dvP names dont want to share it.

What did I do wrong?

Connect-VIServer Vcenter -user administrator@vsphere.local
Get-VDSwitch -Name dvS | Get-VDPortgroup -Name dvP1965 -PipelineVariable pg
#Get-VDPortgroup -PipelineVariable pg | where{$_.VlanConfiguration -is [VMware.VimAutomation.Vds.Types.V1.SingleVlanConfiguration]} |
ForEach-Object -Process {  try{    $tag = Get-Tag -Name $pg.VlanConfiguration.VlanId -ErrorAction Stop
  }  catch {    $tag = New-Tag -Name $pg.VlanConfiguration.VlanId -Category 'VLAN_Tag'  }
New-TagAssignment -Entity $pg -Tag $tag
}

 

 

 

New-Tag : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:5 char:38
+ ...  catch {    $tag = New-Tag -Name $pg.VlanConfiguration.VlanId -Catego ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag

New-TagAssignment : Cannot bind argument to parameter 'Entity' because it is null.
At line:6 char:29
+ New-TagAssignment -Entity $pg -Tag $tag
+ ~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

0 Kudos
LucD
Leadership
Leadership

It looks like you forgot the pipeline symbol at the end of that line


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

0 Kudos