Orange County, Tustin, CA 92780

Enable and Configure ESXi for SNMP Monitoring with PowerCLI

GitHub Repository: https://github.com/virtualization247/vmware-scripts.git
Script Name: snmp_config.ps1
Configure SNMP for ESXi - "Mini Robot" - Krypton / Doragon / CC BY-SA (https://creativecommons.org/licenses/by-sa/3.0)

VMware documentation is excellent, but not if you're a beginner with PowerCLI. While you will find documentation on how to configure SNMP for your ESXi hosts, the way it's presented is not very clear and will leave you scratching your head in confusion since you'll be thinking how does this configure SNMP on all my ESXi hosts?

Simple Network Management Protocol (SNMP) is used to manage and monitor network devices and their functions by setting up a community string using version 1 or 2 or using version 3, which adds both encryption and authentication. The script discussed here will use either version 1 or 2, but not 3.

Warning! If you have monitoring software, be aware that if you have SNMP already running and are updating the community string and restart the service, you'll get a false alert that the ESXi host has rebooted. If you're part of a large team, seeing alerts that an ESXi host has rebooted can send some people into a panic, unless of course you already know that maintenance is going on. Make sure to let your teammates know you are changing SNMP settings.

Assumption(s)Assumption(s) made:
You have a working vCenter Server, you have PowerCLI installed and are connected to vCenter Server via PowerCLI. Additionally, you have credentials for both vCenter Server and the root password for ESXi. You are configuring SNMP for version 1 or 2 using the script below.

If you're ready to setup SNMP for you ESXi hosts, let's get started.

The script below will allow you to multi-select the ESXi hosts you want to configure, and then will set the SNMP community string to your desired value and either start or restart the SNMP service.

The first section of the script is just the title, the version, and comments on what the script does and is very much self-explanatory.

The second section, titled "Variables," is where essential information for credentials is collected. As you can see from the prompt and messages displayed, the script will collect the name of your VMware vCenter Server, ask you to enter your vCenter Server credentials, and finally ask for credentials used to connect directly to your ESXi hosts. Depending on how you have configured your vCenter Server, you may enter your Windows Domain credentials (e.g., This email address is being protected from spambots. You need JavaScript enabled to view it.), or you may use the local Single Sign-on domain and user, such as This email address is being protected from spambots. You need JavaScript enabled to view it. or the customized Single Sign-on domain used during setup. The next credential would be the root account and password for connecting directly to your ESXi host or hosts. Here I'm assuming that you have set all your ESXi hosts to use the same password.

$vcenter = Read-Host -Prompt "Enter your vCenter Server name (FQDN)"
$vcenterCred = Get-Credential -Message "Enter your vCenter Server Credentials"
$esxiCred = Get-Credential -Message "Enter your ESXi root credentials"

The thirds section, titled "Connect to vCenter" does just that: connects to your vCenter Server.

In first line, you are setting some configuration options before connecting to vCenter Server such as ignoring any invalid certificates. I do this because it's very common to find many deployments that don't have properly deployed certificates. Also, because we will not only connect to vCenter Server but your ESXi hosts directly, we don't want any warnings about connecting to multiple servers

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false ` -DefaultVIServerMode Multiple | Out-Null

Here's the full script that you can copy and paste or you can use the GitHub repository URL located at the top of this article to download it.

####### Automate configuring SNMP on ESXi hosts #######
#################### version 1.0  #####################
#######################################################
<# This script will connect to both your vCenter Server,ESXi hosts you select, update the SNMP community stringfor either SNMP v1 or v2, then start or restart theSNMP service for the change to take effect. #>##########################################################################  Variables ############################################################################### $vcenter = Read-Host -Prompt "Enter your vCenter Server name (FQDN)"$vcenterCred = Get-Credential -Message "Enter your vCenter Server Credentials"$esxiCred = Get-Credential -Message "Enter your ESXi root credentials" #####################################################################  Connect to vCenter ########################################################################### Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false `    -DefaultVIServerMode Multiple | Out-NullConnect-VIServer -Server $vcenter -Credential $vcenterCred ######################################################################  Get SNMP Settings ########################################################################### $myHosts = Get-VMHost | Out-GridView -OutputMode MultipleConnect-ViServer $myHosts -Credential $esxiCred$hostSNMP = Get-VMHostSnmp -Server $myHosts.NameWrite-Host "`nThe current settings for your ESXi hosts are as follows:" `    -ForegroundColor Blue$hostSNMP | Select-Object VMHost,Enabled,Port,ReadOnlyCommunities | `    Format-Table -AutoSize

######################################################################  Set SNMP Settings ########################################################################### $communityString = Read-Host "Enter SNMP string."Write-Host "SNMP community string entered is: $communityString `n" `    -ForegroundColor BlueWrite-Host "Updated settings for your ESXi hosts are as follows: `n" `    -ForegroundColor Green$hostSNMP = Set-VMHostSNMP $hostSNMP -Enabled:$true `    -ReadOnlyCommunity $communityString$hostSNMP | Select-Object VMHost,Enabled,Port,ReadOnlyCommunities | `     Format-Table -AutoSize$snmpStatus = $myHosts| Get-VMHostService | `    Where-Object{$_.Key -eq "snmpd"}  ForEach ($i in $snmpStatus) {    if ($snmpStatus.running -eq $true) {        $i | Restart-VMHostService -Confirm:$false | Out-Null    }    else {        $i | Start-VMHostService -Confirm:$false | Out-Null    }} Write-Host "SNMP service has been started on the ESXi host(s)." `    -ForegroundColor Blue$myHosts | Get-VMHostService | Where-Object{$_.Key -eq "snmpd"} | `    Select-Object VMHost,Key,Running | Format-Table -AutoSize ##############################################################  Disconnect from vCenter and ESXi hosts ##############################################################Disconnect-VIServer -Server * -Confirm:$false

Byron Zepeda

Byron Zepeda is a Senior Systems Engineer in Orange County, California, working with VMware vSphere, Citrix Virtual Apps, backups, and storage. As cloud technologies and automation become first-class citizens within IT organizations, he desires to share everything he learns and pass it on to others.