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

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]

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


                  Set the CDP setting on a Standard Virtual Switch.
                  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.
                  PS C:\> Set-VirtualSwitchCDP -VMhost esx1 -MTU 1500 -CDP both -vSwitch vSwitch0
                  PS C:\> Get-VMhost Myhost | Set-VirtualSwitchCDP -vSwitch vSwitch0


    $MTU = 1500,

    $CDP = "both",



       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