You are on page 1of 2

1 # Powershell to configure SNMP - K.

Andreev 20141117
2 # Usage: ./SetSNMP.ps1 -server <server_name> -community <community_string> -target
<target_hostname> -test $true/$false
3 # Adds target_hostname as a SNMP target, community_string as SNMP string on the
server server_name
4 # If parameter -test is $true, nothing is added to the registry. If you ommit the
parameter, nothing is added as well
5
6 param(
7 [string]$server = "",
8 [string]$community = "",
9 [string]$target = "",
10 [bool]$test = $true
11 )
12
13 if (($server -eq "") -or ($community -eq "") -or ($target -eq "")){
14 Write-Host "-------------------------------"
15 Write-Host "Kliment Andreev - November 2014"
16 Write-Host "-------------------------------"
17 Write-Host "Usage: ./SetSNMP.ps1 -server <server_name> -community
<community_string> -target <target_hostname> -test true/false"
18 Write-Host "Adds target_hostname as a SNMP target, community_string as SNMP
string on the server server_name"
19 Write-Host "If parameter -test is $true, nothing is added to the registry"
20 Write-Host ""
21 exit
22 }
23 try {
24 $check = Get-WMIObject Win32_Service -ComputerName $server -ErrorAction Stop |
Where-Object {$_.name -eq "SNMP"}
25 }
26 catch {
27 $Exception = $_
28 Write-Host "SNMP not installed on" $server ", exception:
$($_.Exception.Message)" -ForegroundColor Red
29 }
30 if ($check.Status -eq "OK") {
31 Write-Host "SNMP installed on" $server", proceeding with configuration..."
-ForegroundColor Green
32 $objReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',
$server)
33
34 # Tab Security, Accept SNMP packets from these hosts
35 Write-Host "Accept packets from these hosts:" -ForegroundColor Yellow
36 $Values_counter = 1
37 $objRegKeyPM =
$objReg.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\Permitt
edManagers",$true)
38 $Duplicate = $false
39 while ($true) {
40 $value = $objRegKeyPM.GetValue($Values_counter.ToString())
41 if ($value -eq $target) {
42 $Duplicate = $true
43 }
44 if ($value -eq $null) {break}
45 Write-Host `t $value -ForegroundColor White
46 $Values_counter++
47 }
48 if (!$Duplicate) {
49 Write-Host "===> Adding" $Values_counter $target "to
HKLM\..\SNMP\Parameters\PermittedManagers"-ForegroundColor Green
50 if (!$test) {$objRegKeyPM.SetValue($Values_counter.ToString(), $target)}
51 } else {
52 Write-Host $target "already exists." -ForegroundColor Red
53 }
54 $devnull = $objRegKeyPM.Close
55
56 # Tab Security, Accepted Community Names
57 Write-Host "Accepted Community Names:" -ForegroundColor Yellow
58 $objRegKeyVC =
$objReg.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ValidCo
mmunities",$true)
59 $Duplicate = $false
60 foreach($value in $objRegKeyVC.GetValueNames()){
61 Write-Host `t $value, $objRegKeyVC.GetValue($value).ToString()
62 if ($value -eq $community){
63 $Duplicate = $true
64 }
65 }
66 if (!$Duplicate) {
67 Write-Host "===> Adding " $community "to
HKLM\..\SNMP\Parameters\ValidCommunities" -ForegroundColor Green
68 if (!$test) {$objRegKeyVC.SetValue($community, 8)}
69 } else {
70 Write-Host $community "already exists." -ForegroundColor Red
71 }
72 $devnull = $objRegKeyVC.Close
73
74 # Tab Traps, Community Name
75 $objRegKeyCN =
$objReg.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\TrapCon
figuration",$true)
76 $devnull = $objRegKeyCN.CreateSubKey($community)
77 $devnull = $objRegKeyCN.Close
78
79 # Tab Traps, Trap Destinations
80 Write-Host "Trap destinations:" -ForegroundColor Yellow
81 $Values_counter = 1
82 $strKey =
"SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\TrapConfiguration\\"
83 $strKeyTD = $strKey + $community
84 $objRegKeyTD = $objReg.OpenSubKey($strKeyTD,$true)
85 $Duplicate = $false
86 foreach($value in $objRegKeyTD.GetValueNames()){
87 $value_name = $objRegKeyTD.GetValue($value).ToString()
88 Write-Host `t $value, $value_name
89 $Values_counter++
90 if ($value_name -eq $target){
91 $Duplicate = $true
92 }
93 }
94 if (!$Duplicate) {
95 Write-Host "===> Adding" $values_counter $target "to
HKLM\..\SNMP\Parameters\TrapConfiguration\"$community -ForegroundColor Green
96 if (!$test) {$objRegKeyTD.SetValue($Values_counter.ToString(), $target)}
97 } else {
98 Write-Host $target "already exists." -ForegroundColor Red
99 }
100 $devnull = $objRegKeyTD.Close
101 $devnull = $objReg.Close
102 }

You might also like