In this post we will be going through deploying new SNMP configuration to a list of ESXi hosts using PowerCLI. We can add SNMP using ssh and esxcli commands but this will required SSH to be enabled and connecting to each host.
We can use Set-VMHostSNMP command to set the SNMP configuration by conecting to the host using connect-viserver which uses https to connect and does not required SSH to be enabled.
First we need to connect to the ESXi host
Connect-VIServer -Server esxihost.domain.local
use a local account like root to connect

If you get a certificate error and can’t connect you might need to update the PowerCLI Configuration or install the root cert to trust the self singed cert of the ESXi hosts.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Once connected we will be using the below set of command to configure the SNMP settings.
Get-VMHostSnmp | Set-VMHostSnmp
First we will enabled and set the community string
Get-VMHostSnmp | Set-VMHostSnmp -Enabled:$true -ReadOnlyCommunity communityname

Next we will set the target that traps will be sent.
Get-VMHostSnmp | Set-VMHostSnmp -TargetCommunity communityname -TargetHost snmp.domain.local -TargetPort 162 -AddTarget

To test I have setup snmp on Ubutu

Now that we have set of command we can create the script. I will be doing a loop through each host and configuring the SNMP.
I also added in some if statement to check if SNMP is already enabled and to check if the target host matches the one that is set in the parameters.

The full script can be downloaded from the below github link.
https://github.com/TheSleepyAdmin/Scripts/blob/master/VMware/Config/SNMP/Set-ESXiSNMP.ps1
Below is an example of what the csv file should look like if you want to use a different heading the script will need to be updated.

Each of the values will be called as parameter to allow easy re-use below is an example of the SNMP script running against my test hosts.

Using PowerCli can be a quicker way to set and enabled SNMP on a list of hosts rather than having to SSH and use esxcli command. This can also be used to update the existing SNMP configuration to a new traps target.