Configure SNMP with Powershell

Standard

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.

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

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

"10.0.0.1, 10.90.0.15"

Community
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 '10.0.0.1, 10.0.0.2' -Community 'com1, com2'

Param(
	[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
			$managerCount++

			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')
				$managerCount++
			}

			# 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
					$managerCount++
				}
			}

			# 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
					$communityCount++
				}
			}
		}
		else
		{
			write-host -foregroundcolor Red "SNMP Service not present!"
		}
	}
	else
	{
		write-host -foregroundcolor Red "Server $Server could not be reached!"
	}
}

Write-host -foregroundcolor Green “Completed”