Mephesto
Contributor
Contributor

Rename a PortGroup

Hello

A requirement has arisen in our estate where an area needs to have a portgroup renamed. We have a one-size-fits-all configuration script that will continue to be used without amendment but the idea is to have a secondary script that will be run later when the host is known to be headed for this other area. Unfortunately this other area wants their portgroups to be renamed differently from the "standard". I am quite happy adding new groups and removing surplus ones but I am not so good with renaming. The UpdatePortGroup Method would appear to support the changing of a name but I haven't been successful in getting it to work. I am sure it is down to my syntax and setting up of the parameters so any guidance that people could give me would be appreciated.

Thanks

James

Reply
0 Kudos
LucD
Leadership
Leadership

Try this, it should do the trick.

$hostname = <ESX-hostname>
$vswitch = <vSwitch-name>
$pgname = <old-PortgroupName>
$newpgname = <new-PortgroupName>

$net = Get-View (Get-VMHost $hostname | Get-View).configManager.networkSystem

foreach($pg in $net.NetworkInfo.Portgroup){
  if($pg.Spec.VswitchName -eq $vswitch -and $pg.spec.Name -eq $pgname){
    $spec = $pg.Spec
  }
}
$spec.name = $newpgname
$net.UpdatePortGroup($pgName,$spec)


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

View solution in original post

Reply
0 Kudos
Mephesto
Contributor
Contributor

That is what I call a turnaround! That did the job wonderfully, thanks. I was almost there, it was just the appreciation of the $spec element that I was missing. I am still not 100% with it but now that I have a working example I can have a play around and improve my understanding (hopefully).

Thanks again.

Reply
0 Kudos
LucD
Leadership
Leadership

For a lot of methods that require a "spec" object, the object is already there in some other objects.

That's what I did here, in the NetworkSystem object all portgroups are there.

With a loop I check them all and just copy the correct HostPortGroup object.

Then I just change the property (in this case the name) that is needed and feed it is a parameter to the UpdatePortGroup method.

Hope this clarifies a bit hwat the script does.


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

Reply
0 Kudos
dalo
Hot Shot
Hot Shot

Hi Luc

I would try this script, but I've a problem to understand one thing:

Why is the section

$spec.name = $newpgname

$net.UpdatePortGroup($pgName,$spec)

not in the if statement? In my opinion the change should only made if the portgrop matches the $pgname or are I'm wrong?

Daniel

Reply
0 Kudos
LucD
Leadership
Leadership

In the If condition I run through all the portgroups until I find the one that belongs to the correct vSwitch and has the correct portgroupname.

Then I copy the specifications of that portgroup ($pg.spec) to a new variable ($spec).

The only thing we want to to change is the name of the portgroup. That's the $spec.name = $newpgname statement.

And finally I call the UpdatePortGroup method to actuallt make the change.

I could have put the 2 last statements in the If block but since I expect only 1 portgroup on a specific vSwitch with a specific name I can put those 2 statements also outside the If block.


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

Reply
0 Kudos
aerodevil
Hot Shot
Hot Shot

When renaming the portgroup with this method does it disconnect any VMs that are currently using that portgroup forcing you to reset the VMs assigned portgroup?

Josh Atwell

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
Reply
0 Kudos
dalo
Hot Shot
Hot Shot

Thank you for the explanation.

Now I see my problem, I did not realized that the foreach loop was already finished there. Now the logic is clear to me.

I'm learning with every script...

Reply
0 Kudos
dalo
Hot Shot
Hot Shot

No there is no network disconnect of the VM, but you have to select the new portgroup in the VM properties after the script.

Reply
0 Kudos