Contents

Configure SNMP with Powershell

Contents

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 need to be added to the SNMP service. This can be a comma separate 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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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 need to be checked. Has to be 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 the 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 number 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