Modify SNMP with ESXCLI V2

If you’ve ever used the Get-EsxCli command then you are familiar with the the positional variables and the army of $nulls. For example, setting SNMP in the past would consist of something like this:

$esxcli.system.snmp.set($null,’$community’,$true,$null,$null,$null,$null,’162′,$null,$null,$null,$null,$null,’192.168.22.64/public’,$null,$null)

Each parameter (16 for SNMP as of ESXi 6.7) needs to have either a value or $null. This is tedious on a few levels. First, it’s just cumbersome to make sure your variable/$null combination is correct. If you place your variable where a $null should be, then your command isn’t going to work.

Second is upgrades! As I stated earlier, ESXi 6.7 has 16 parameters for setting SNMP. What about other versions of ESXi? Well 5.5 actually has 17 parameters. So now your script is not backwards compatible.

Enter V2, and the power of the argument. Long gone are the days of making sure all your $nulls are in order. Our first step is to see what are options are.

$esxcli = Get-EsxCli -VMHost $vmhost -V2
$esxcli.system.snmp.get.Invoke()
authentication : 
communities    : 
enable         : true
engineid       : 00000063000000a100000000
hwsrc          : indications
largestorage   : true
loglevel       : info
notraps        : 
port           : 161
privacy        : 
remoteusers    : 
syscontact     : 
syslocation    : 
targets        : 
users          : 
v3targets      : 

We want to set the community, and target strings:

$snmpargs = $esxcli.system.snmp.set.CreateArgs()
$snmpargs.communities = $community
$snmpargs.targets = "192.168.22.64/public"

Next we execute the command with Invoke and our $snmpargs variable:

$esxcli.system.snmp.set.Invoke($snmpargs)

And re-run the $esxcli.system.snmp.get.Invoke() with no arguments to the output:

authentication : 
communities    : {icanhazsnmp}
enable         : true
engineid       : 00000063000000a100000000
hwsrc          : indications
largestorage   : true
loglevel       : info
notraps        : 
port           : 161
privacy        : 
remoteusers    : 
syscontact     : 
syslocation    : 
targets        : {192.168.22.64@161 public}
users          : 
v3targets      : 

Ironically, I only switched to V2 after having issues with the original “version 1”. Since then, it’s good bye to $nulls for me!

Leave a Reply

Your email address will not be published. Required fields are marked *