2012-02-23

How to Set CDP on a vSwitch–the #PowerCLI Way

We all know that you get CDP information in a number of ways (also PowerCLI), KB 1007069 provides a number of ways to do it.

But how would you go about setting CDP on the vSwitch?

That can be done on the host itself in a number of ways:

esxcfg-vswitch –B both vSwitch0

but also with esxcli

esxcli network switch standard set –c both –v vSwitch0

That got me thinking…. esxcli… where had I used that before – of course Netstat for ESXi.

So why not try and do the same here?

The quick and dirty way (I will update later with a proper function)

$esxcli = Get-EsxCli -VMHost $myhost
$esxcli.network.vswitch.standard.set("both","1500","vSwitch0")

Why all 3 Parameters and where did they come from?

$esxcli.network.vswitch.standard | gm

The set method is described as follows:

Name          MemberType    Definition
set              CodeMethod   boolean set(string cdpstatus, long mtu, string vswitchname)

And where do get the values that are valid for “cdpstatus” ? esxcli of course

# esxcli network vswitch standard set

Error: Missing required parameter -v|--vswitch-name

Usage: esxcli network vswitch standard set [cmd options]

Description:
  set                   This command sets the MTU size and CDP status of a given virtual switch.

Cmd options:
  -c|--cdp-status=<str> The CDP status of the given virtual switch. It can be 'down', 'listen', 'advertise' or 'both'
  -m|--mtu=<long>       The MTU size of the given virtual switch.
  -v|--vswitch-name=<str>
                        The name of virtual switch to apply the configurations. (required)

Update: March 06, 2012

I promised to update with a proper function on how to do this, and here it is:

Function Set-VirtualSwitchCDP {

      <#

            .SYNOPSIS
                  Set the CDP setting on a Standard Virtual Switch.
            .DESCRIPTION
                  Using the Get-Esxcli cmdlet you can changed the CDP Settings 
                  on virtual switch on an ESX host.
            .NOTES Author: Maish Saidel-Keesing
            .PARAMETER  VMHost
                  ESX server to perform the function.
            .PARAMETER  MTU
                  The MTU settings of the vSwitch, default is 1500.Valid values are 
                  between 1500-9000
            .PARAMETER  CDP
                  The CDP setting for the vSwitch. 
                  Valid values are 'down', 'listen', 'advertise' or 'both'.
            .PARAMETER  vSwitch
                  The Name of the Standard vSwitch on which to perform the action.
            .EXAMPLE
                  PS C:\> Set-VirtualSwitchCDP -VMhost esx1 -MTU 1500 -CDP both -vSwitch vSwitch0
            .EXAMPLE
                  PS C:\> Get-VMhost Myhost | Set-VirtualSwitchCDP -vSwitch vSwitch0
            .LINK
                  http://technodrone.blogspot.com/2012/02/how-to-set-cdp-on-vswitchthe-powercli.html
      #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
    Param(
    [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True)]
    [String]
    $VMHost,

    [Parameter(Position=1)]
    [ValidateRange(1500,9000)]
    [Int]
    $MTU = 1500,

    [Parameter(Position=2)]
    [ValidateSet("down","listen","advertise","both")]
    [String]
    $CDP = "both",

    [Parameter(Position=3,Mandatory=$True)]
    [String]
    $vSwitch
    )

     

      Process
      {
       if ($pscmdlet.ShouldProcess($VMHOST,"Updating $vSwitch with MTU $MTU and CDP setting of $CDP"))      {
                  foreach ($hostobject in (Get-VMHost $VMHost)) {
                        $esxcli = Get-EsxCli -VMHost $hostobject
                        $esxcli.network.vswitch.standard.set($CDP,$MTU,$vswitch)
                        $esxcli.network.vswitch.standard.list()
                  }
            }
      }
}