Has been done before, but things have changed with 6.5 and Get-EsxCli -V2, so I thought I’d share an updated script.   This makes sure you have the same SNMP settings on all hosts.  This is kind of a response to comment on this page

Uses also the newer method of loading the VmWare PowerCLI modules.

#require -module VMware.VimAutomation.Core

$vcsa = "vcentre.yourdomain.local" # Your vSphere server
$snmpdROCommunities = "PuBl1c" # v2c RO comminuties (can comma seperated list of up to 10)
$syscontact = "administrator@vsphere.local" # Set the snmp attributes
$syslocation = "My DataCentre"
$port = "161" #Change the port if you want
$me = Get-Credential -Message "vSphere Admin"

Connect-VIServer -Server $vcsa -Credential $me | Out-Null

$vmhosts = Get-VMHost | where {$_.ConnectionState -eq "Connected"}
foreach ($vmhost in $vmhosts) {
  #Set SNMP settings using new V2 esxcli interface
  $esxcli = Get-EsxCli -VMHost $vmhost -V2
  $arguments = $esxcli.system.snmp.set.CreateArgs()
  $arguments.communities = $snmpdROCommunities
  $arguments.enable = $true
  $arguments.port = $port
  $arguments.syscontact = $syscontact
  $arguments.syslocation = $syslocation

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

  #Quickly dump out the new config
  write-host $vmhost.Name
  $esxcli.system.snmp.get.Invoke()

  $snmpd = Get-VMHostService -VMHost $vmhost | where {$_.Key -eq "snmpd"}
  #Start SNMP service - if not configured properly it will error here
  $snmpd | start-VMhostservice
  #configure Auto start and stop with host
  $snmpd | set-VMhostservice -Policy "On"
}

Disconnect-VIServer -Server $vcsa
Setting SNMP on all ESXi 6.5 hosts in a cluster

4 thoughts on “Setting SNMP on all ESXi 6.5 hosts in a cluster

    • 2020-10-13 at 8:42 am
      Permalink

      Only if you want to reset the SNMP settings to factory default!

      Reply
  • 2020-10-22 at 2:49 pm
    Permalink

    snmp target in not mentioned in that script

    Reply
    • 2020-11-26 at 9:13 am
      Permalink

      That’s right. This is for SNMP get/set responses only. i.e active monitoring. SNMP target is only needed when you want the ESXi host to send traps to some monitoring host.

      Reply

Leave a Reply to admin Cancel reply

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