Automate SNMP configuration on multiple ESXi hosts

I have created a PowerCLI script that can be used for applying SNMP configuration on multiple ESXi hosts of a vSphere Cluster by replicating configuration from a reference ESXi host.

Leave a comment with your email address if you have a question and I will get back to you soon.

Enjoy the script 🙂

  1. <#
  2.   configure-snmp-on-esxi.ps1
  4.   Configure SNMP settings on multiple ESXi hosts using a reference host settings
  6.   .History.
  7.   2020/04/09 - 0.1 - Reza Rafiee - First version
  9.   .Variables.
  10.   $VC: vCenter Server
  11.   $targethosts: Target ESXi host cluster
  12.   (to apply on single ESXi host refer to line30)
  13.   $refesxhost : Reference ESXi host
  15. #>
  17. $VC="vCenter Server Name"
  18. $targethosts="Target cluster name"
  19. $refesxhost = "Reference ESXi host name"
  23. Connect-viserver $VCServer
  24. $refhost = get-vmhost $refesxhost
  25. $refesxcli = Get-EsxCli -VMhost $refhost -V2
  26. $snmp=$refesxcli.system.snmp.get.invoke()
  27. write-host "SNMP configuration on " $refesxhost," (Refernce Host): "
  28. $snmp
  30. $vmhosts = get-cluster -name $targethosts | get-vmhost
  31. <#If you want to apply the snmp config on a single host
  32.  then enter ESXi host name for $targethosts variable and
  33.  replace the above line with the below line:
  35.  $vmhosts = get-vmhost -name $targethosts
  37.  #>
  40. foreach ($vmhost in $vmhosts){
  42. $esxcli = Get-EsxCli -VMHost $vmhost -V2
  44. <#Reset SNMP settings to factory default on the target host prior to
  45. reconfigure SNMP settings on that host#>
  46. $snmpreset = $esxcli.system.snmp.set.CreateArgs()
  47. $snmpreset.reset = $true
  48. $esxcli.system.snmp.set.Invoke($snmpreset)
  50. write-host "SNMP settigs has been reset to default on $vmhost"
  51. #SNMP settings reset complete
  53. $esxcli = Get-EsxCli -VMHost $vmhost -V2
  54. $arguments = $esxcli.system.snmp.set.CreateArgs()
  56. #The below arguments (if statements) cannot be null hence we skip the null ones
  58. if ($snmp.communities -ne $null) {
  59. $arguments.communities = $snmp.communities
  60. }
  62. if ($snmp.engineid -ne "$null") {
  63. write-host "engineid is nt null"
  64. $arguments.engineid = $snmp.engineid
  65. }
  67. if ($snmp.targets -ne $null) {
  68. $arguments.targets = $snmp.targets
  69. }
  71. if ($snmp.users -ne $null) {
  72. $arguments.users = $snmp.users
  73. }
  75. if ($snmp.privacy -in ("none", "AES128")) {
  76. $arguments.privacy = $snmp.privacy
  77. }
  79. if ($snmp.remoteusers -ne $null) {
  80. $arguments.remoteusers = $snmp.remoteusers
  81. }
  83. if ($snmp.authentication -in ("none", "MD5", "SHA1")) {
  84. $arguments.authentication = $snmp.authentication
  85. }
  87. if ($snmp.v3targets -in ("none", "auth", "priv")) {
  88. $arguments.v3targets = $snmp.v3targets
  89. }
  91. $arguments.hwsrc = $snmp.hwsrc
  92. $arguments.largestorage = $snmp.largestorage
  93. $arguments.loglevel = $snmp.loglevel
  94. $arguments.notraps = $snmp.notraps
  95. $arguments.enable = $snmp.enable
  96. $arguments.port = $snmp.port
  97. $arguments.syscontact = $snmp.syscontact
  98. $arguments.syslocation = $snmp.syslocation
  100. $esxcli.system.snmp.set.Invoke($arguments)
  102. $newsnmp=$esxcli.system.snmp.get.Invoke()
  103. write-host "SNMP configuration on", $vmhost, ": "
  104. $newsnmp
  106. }

Connect vNIC on a VM to network using command line

Once upon a time I had an ESXi host in disconnected state and the management services were out of order and even restarting management services couldn’t help out to get it back to manageable state.

While the Host was partially manageable we had to connect a network interface of a VM to network but the only option was command line. The below commands did the job:

You will need to find the VM ID and the vNIC device ID as well using the below two commands:

vim-cmd vmsvc/getallvms | grep "VM_Name"
vim-cmd vmsvc/get.configuration "VM_ID"

Then you can run the below command to connect/disconnect the vNIC:

vim-cmd vmsvc/device.connection true|false


How to know if ESXi or Xen server is using UEFI or Legacy boot mode

There might be times that you need to know if the ESXi host boot mode is set to UEFI or Legacy and obviously one option is to reboot the host and check the boot mode from BIOS. But it requires a downtime and sometimes it’s not an option in critical production environment.

Here is a simple command in both ESXi and Xen server that you can run to identify the boot mode without rebooting the server:


to check boot type of esx run this command from putty SSH:

vsish -e get /hardware/firmwareType


to check boot type of a xen host check for the EFI folder under /sys/firmware/

open up the xen host console and run the check the contents of /sys/firmware folder by running the below commands:

cd /sys/firmware/

if it returns a folder labled EFI then it’s UEFI boot. Otherwise it’s Legacy.

Some useful tips for powering off VMs from ESXi Command Line

There might be occasions that you need to power off VMs from ESXi command line for some reason. knowing how to do it can save you time and effort from dealing with corrupted virtual disks and VMs. For instance an ESXi host loses all network connectivity due to a driver and firmware compatibility issue and you have to reboot the host but the VMs are still running with no network connectivity. In that case it will be safer if you gracefully shutdown the VMs and then put the host in maintenance mode and then reboot the ESXi host. Let’s see how to do it:

First of all you will need to grab Virtual Machines ID by running the below command:

vim-cmd vmsvc/getallvms

Below command returns the VM’s power state:

vim-cmd vmsvc/power.getstate VMID

Now you can try grcefully shutdown the VM. If the VM doesn’t respond to graceful shutdown in couple of minutes then you can forcefully power it off by the second command:

#For gracefull shutdown:
vim-cmd vmsvc/power.shutdown VMID
#For forcefully powering off the VM:
vim-cmd vmsvc/ VMID

The alternative way of doing the power off action is using ESXCLI command:

esxcli vms vm list
esxcli vms vm kill --type=[soft,hard,force] --world-id= WorldNumber

If neither of the above work then you will need to kill the VM process using below commands:

#find the VM World ID
esxcli vm process list

#Kill the VM process
esxcli vm process kill --type=[soft,hard,force] --world-id= WorldNumber

Note You can kill VM process using ps and kill commands similar to “esxcli vm process” if esxcli is not functioning.

Once you power off all you VMs then you can put the ESXi host in maintenance mode and power it off or reboot it using the following command:

#put the host in maintenance mode
esxcli system maintenanceMode set --enable true

#Power off the ESXi host
esxcli system shutdown [poweroff,reboot] --delay "seconds" --reason "description"