Configure SNMP with Powershell


Powershell can do a lot of stuff and with the right libraries you can do about anything.

I had to configure the SNMP service on a bunch of servers and I wasn’t going to do this by hand for over 80 servers.

It uses three parameters for configuring the service.

Path to file that contains all the servers. The text file must have only one server per row.

The managers that need to be added to the SNMP service. This can be a comma seperated string with  the following format:


This parameter works the same as the Manager-parameter with the same format.

The code

# PowerShell script to edit the SNMP registry
# Author: Sander Stad
# Version: 1.0 June 2011 tested on Powershell v2.0
# Usage: SNMConfig -ServerList 'c:\servers.txt' -Manager ',' -Community 'com1, com2'

	[string] $ServerList, # List of servers that needs to be checked. Has to a text file!
	[string] $Manager, # List of managers to add
	[string] $Community # List of communties to add

function Ping-Host {
	param( [string]$HostName, [int32]$Requests = 3)

	for ($i = 1; $i -le $Requests; $i++) {
		$Result = Get-WmiObject -Class Win32_PingStatus -ComputerName . -Filter "Address='$HostName'"
		Start-Sleep -Seconds 1
		if ($Result.StatusCode -ne 0) {return $FALSE}
	return $TRUE

$Servers = Get-Content $ServerList

# Run through each server in the server list
foreach( $Server in $Servers){
	Write-host -foregroundcolor Cyan "Server Name : $Server"

	# Check if host can be reached
	if (Ping-Host $Server)
		$serviceCheck = Get-WmiObject -computer $Server Win32_Service -Filter "Name='SNMP'" -ErrorAction SilentlyContinue

		# Check if the SNMP Service is installed
		if($serviceCheck.Name -eq 'SNMP')
			# Create the registry object
			$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Server)
			$regKeyMan = $reg.OpenSubKey('SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers', $true)
			$regKeyCom = $reg.OpenSubKey('SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities', $true)

			# Get the amount of managers and communities already present
			$managerCount = $regKeyMan.ValueCount
			$communityCount = $regKeyCom.ValueCount
			$managerArray = @()
			$communityArray = @()

			# Get all the present values and save them in the array
			for($i = 1; $i -le $managerCount; $i++)
				$managerArray += $regKeyMan.GetValue($i)

			for($i = 0; $i -le $communityCount; $i++)
				$communityArray += $regKeyCom.GetValue($i)

			# Increase counters

			write-host $communityArray

			#Check if the localhost can query the SNMP service.
			if(($managerArray -contains 'localhost') -eq $false)
				Write-Host -foregroundcolor Blue " - Adding manager key: localhost"
				$regKeyMan.SetValue($managerCount, 'localhost')

			# Run through each of the managers and check the registry if the entry
			# already exists.
			foreach ($m in $Manager){
				if(($managerArray -contains $m) -eq $false)
					Write-Host -foregroundcolor Blue " - Adding manager key: $m"
					# Add the manager if it doesn't exist
					$regKeyMan.SetValue($managerCount, $m, [Microsoft.Win32.RegistryValueKind]::String)
					#Increase the manager counter

			# Run through each of the communities and check the registry if the entry
			# already exists.
			foreach ($c in $Community)
				if(($communityArray -contains $c) -eq $false)
					Write-Host -foregroundcolor Blue " - Adding community key: $c"

					# Add the community if it doesn't exist
					$regKeyCom.SetValue($c, 4, [Microsoft.Win32.RegistryValueKind]::DWord)

					# Increase the community counter
			write-host -foregroundcolor Red "SNMP Service not present!"
		write-host -foregroundcolor Red "Server $Server could not be reached!"

Write-host -foregroundcolor Green “Completed”



7 thoughts on “Configure SNMP with Powershell

  1. Bob

    Hi, I need to enable snmp across 40servers running SERVER 2012

    cant get your code to run – any suggestions?
    Error is cannot find servers txt file
    great if you could help

    • Sander Stad

      You have to be a little more specific.
      The code is designed to take arguments like this:
      SNMPConfig -ServerList ‘c:\servers.txt’ -Manager ‘,’ -Community ‘com1, com2’

      If the error says the file can’t be found than there the file isn’t there of you have some privilege issues that the script can’t read or find the file.

      If you still have some issues pleasy supply as much information about your issue as possible.

  2. Per


    I’m new to PS so please bare with me if I’m totally wrong.
    When I tried your script providing the -ServerList as a .txt file as you suggest I get an error saying “Server C:\temp\snmp\Servers.txt could not be reached!”
    But if I instead of calling the .txt writes my server IP directly in the call it’s working.
    I can’t see in your code that you are opening the .txt file so made me wonder.

    Anyhow, thanks for posting this amazing script! 🙂

  3. Per

    Hi again,

    I see that for multiple values to add e.g. Managers ‘,’ there will be one entry containing both ip’s. Wouldn’t you want a separate entry for each address? Somehow I need to split the comma delimited string into single addresses and add each after that right?


  4. Eva

    Hi, this is a handy script and the best I’ve seen to do this task yet. However is there a way to do this for Windows 2003 servers?

    In a mixed environment it would be great to be able to run a single script that will check the OS and then run the proper portion of the script to enable and configure snmp based on the OS version.

Leave a Reply

Your email address will not be published.