NSH & PowerCLI Script to change VM PortGroup

Version 1
    Share:|

    Script to connect a VM to the specified portgroup
    It supports both regular VSwitch portgroups and DVS portgroups
    The Script would first try and change the portgroup to a regular vSwitch portgroup. If this fails, it would try and change it to a DVS portgroup
    Arguments

    Script to connect a VM to the specified portgroup

    It supports both regular VSwitch portgroups and DVS portgroups

    The Script would first try and change the portgroup to a regular vSwitch portgroup. If this fails, it would try and change it to a DVS portgroup

     

    Arguments



     

    • vcServerName: The Virtual Center server name
    • vcUserName  : The VMware SDK login user
    • vcPassword  : The VMware SDK login password
    • vmName      : The VM name whose NIC is to be connected
    • nicName     : The name of the NIC to connect to the portgroup. Example, 'Network adapter 1'
    • vPgName     : The VMware portgroup to connect the VM to (could be regular VSwitch or DVS portgroup)

     

    To invoke this script from NSH, we need to create an NSH wrapper for this script:

    Create a new NSH script with the following contents:

         powershell portChange.ps1 {$1} {$2} {$3} {$4} {$5} {$6}

         exit $?



     

    Note: For this to work, one must prepend the global PATH variable with the path of the portChange.ps1 script. This is a powershell requirement.

     

    Invoke the script in the following way: (Supposing the name of the NSH script is portChange.nsh)

    nsh /d/Scripts/PowerCLI/portChange.nsh vCenterName vCenterUserName vCenterPassword VM-Name "Network Adapter 1" "VM Network"