Home > ESXi, PowerShell, SNMP, vSphere > HOWTO: Use vSphere PowerCLI to control Get-EsxCLI for setting SNMP on ESXi v5.1U1 hosts.

HOWTO: Use vSphere PowerCLI to control Get-EsxCLI for setting SNMP on ESXi v5.1U1 hosts.

So today I had a need to set SNMP parameters for all ESXi hosts in vCenter.  Easily enough done at the SSH command line:

esxcli system snmp set –communities nw_public –enable yes

That’s going to set the community and enable SNMP.   Everything else is default, and we’re not setting up any sort of security.  SNMPv1/v2 don’t have security or encryption, vSphere v5.1 only supports setting a remote IP for a *trap* not a *get*, so we can’t do that, and we’re not using SNMPv3 (which has security and encryption built in).  So this is all we really need to set.

PowerCLI has no equivalent that I’m immediately aware of.  However, since vSphere v4.1, the functionality of “esxcli” at the command prompt has been available via PowerCLI with the “Get-EsxCli” commandlet. However, it’s not really easy to wrap your head around at first.  The best way to describe it is once you start a Get-EsxCli session, you continue to execute commands against it until you exit out.  Think RSH or Remote PowerShell or PSexec in similarity.

So here we have the script at hand.  I’ll break it down after:

===== Set-ESXISNMP.ps1 =====

$esxlist = get-vmhost

$communities = “nw_public”

$enable = $true

$port = 161

foreach($item in $esxlist){

Connect-VIServer -Server $item -User root -Password “<rootpassword>”

$esxcli = Get-EsxCli -VMhost $item

$esxcli.system.snmp.set($null,”$communities”,”$enable”)

$esxcli.system.snmp.get()

}

===== Set-ESXISNMP.ps1 =====

# Get the list of VMhosts in vCenter.  This assumes you’re already done “Connect-VIServer NW-VC1 etc”.  I should have done better here.  Anyway……  this sticks it into an array called $esxlist.

$esxlist = get-vmhost

# next we define some variables, straightforward enough.

$communities = “nw_public”

$enable = $true

$port = 161

# Now we start a “foreach” loop, executing against each $item in the $esxlist

foreach($item in $esxlist){

# the first command we’ll run is connect-viserver –server $item (servername) with user and password added.

# user/password can be exported to saved credentials, but this is just as easy for now.

Connect-VIServer -Server $item -User root -Password “<rootpassword>”

# This is the way the world sets up a Get-EsxCli session, so I did it the same way. 

$esxcli = Get-EsxCli -VMhost $item

# we’re going to use “.” Between the same options used at the SSH command line to move through the command

# in the () we’re going to put values for Field1, Field2, Field3.  How do we know the fields?  I’ll get to that…..

$esxcli.system.snmp.set($null,”$communities”,”$enable”)

# Then we do a quick GET of the same thing.  Here, you’ll see the fields. 

$esxcli.system.snmp.get()

}

Output looks like:  (here you will see how I knew what the fields were)

nw-esxi01.netwise.ca      443   root

true

(field1…) authentication :

(field2…) communities    : {nw_public}

(field3…) enable         : true

engineid       : 00000063000000a10a2000d3

hwsrc          : indications

loglevel       : info

notraps        :

port           : 161

privacy        :

remoteusers    :

syscontact     :

syslocation    :

targets        :

users          :

v3targets      :

nw-esxi02.netwise.ca     443   root

true

authentication :

communities    : {nw_public}

enable         : true

engineid       : 00000063000000a10a2400d3

hwsrc          : indications

loglevel       : info

notraps        :

port           : 161

privacy        :

remoteusers    :

syscontact     :

syslocation    :

targets        :

users          :

v3targets      :

So if you wanted to set $port as well, which is field 8, you would:

$esxcli.system.snmp.set($null,”$communities”,”$enable”,$null,$null,$null,$null,”$port$”)

Where “$port” is now in position8, as spaced out by the $null.  $null means you are not editing that particular spot.  I’m not sure if you could do it with a command line you can at the actual command line where you do “-port=161” or “-communities=”nw_public”, but this way works for now.

This is my first really heavy usage into Get-EsxCli, but I’m sure I can do a LOT more with this now!

Advertisements
Categories: ESXi, PowerShell, SNMP, vSphere

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: