Home > PowerClI, PowerShell Scripting > Unique portgroup fiddling with #Powercli.

Unique portgroup fiddling with #Powercli.

As my peers seem to like to flout convention, particularly naming conventions. I was left with a strange automation challenge this week.

with a new service coming online in my company, I had 12 vlan portgroups to add to over 30 ESXi machines. Simple you say, just pipe your hosts into a new virtualswitch query then into a new virtual port group command.

Did i mention the virtual switch has a different name per host? Its vswitch0 on some, vswitch1 on others, and even vswitch2 on one.Arse.

after a few hours of various queries, below is the little scripty I was left with. I did consider putting this into a function, but I didnt know where to begin when it came to a name…

add-PortGroupInSwitchWhereOtherPortGroupExists? … bleh.

Below the variables are pretty self explanatory and it works flawlessly. Remember to install the powercli commands, import the module and connect to the vi server first!

#Variables
 $findportgroupname="GWA4"
 $NewPortGroupName="Citrix7"
 $NewPortGroupVlanId="306"

foreach ($portgroup in get-virtualportgroup | where {$_.name -eq $findportgroupname}){
 get-vmhost -id $portgroup.vmhostid |
  get-virtualswitch -name $portgroup.virtualswitchname |
   new-virtualportgroup -name $NewPortGroupName -Vlanid $NewPortGroupVlanId
 }

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: