Automation

 View Only
  • 1.  Rename a PortGroup

    Posted Feb 16, 2009 02:11 PM

    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



  • 2.  RE: Rename a PortGroup
    Best Answer

    Posted Feb 16, 2009 02:32 PM

    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)
    



  • 3.  RE: Rename a PortGroup

    Posted Feb 16, 2009 02:54 PM

    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.



  • 4.  RE: Rename a PortGroup

    Posted Feb 16, 2009 03:01 PM

    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.



  • 5.  RE: Rename a PortGroup

    Posted Dec 01, 2009 04:08 PM

    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



  • 6.  RE: Rename a PortGroup

    Posted Dec 01, 2009 04:49 PM

    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.



  • 7.  RE: Rename a PortGroup

    Posted Dec 01, 2009 07:33 PM

    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...



  • 8.  RE: Rename a PortGroup

    Posted Dec 01, 2009 07:26 PM

    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



  • 9.  RE: Rename a PortGroup

    Posted Dec 01, 2009 07:36 PM

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