#powershell SNMP Services Administration (Part 4)

This script gathers the pieces discussed previously into a reusable PowerShell function Set-SNMP for the purpose of configuring SNMP settings on Windows machines.

#requires -version 3.0
###############################################################################
# Copyright (c) 201x-2018 leedesmond.com
# All Rights Reserved. Use at your own risk and responsibility.
# Version 1.00
#
# Function with self explanatory parameters to configure SNMP settings on
# Windows machines.
#
###############################################################################
#
function Set-SNMP(
$communityName = “MySnmpCommunity”,
$hostFqdn = “fe01.leedesmond.com”,
[switch]$tabTraps = $true, [switch]$tabSecurity = $true,
[switch]$sysServices, $sysServicesValue = 0,
[switch]$EnableAuthenticationTraps, $EnableAuthenticationTrapsValue = 0
)
{
$snmp = “HKLM:\System\CurrentControlSet\Services\SNMP”
$snmpParameters = “$snmp\Parameters”

if ($tabTraps)
{
$TrapConfiguration = “$snmpParameters\TrapConfiguration”
#create item ($server)
if (-not (Test-Path $TrapConfiguration))
{
New-Item -Name $communityName `
-Path $TrapConfiguration
}

$i = 0
$i = (Get-ItemProperty `
“$TrapConfiguration\$communityName” |
Measure).count
$i++

#repeat to add more properties to this item ($server)
New-ItemProperty -Name $i `
-Value $hostFqdn `
-Path “$TrapConfiguration\$communityName”

#check
#cd $snmpParameters
#dir “.\TrapConfiguration”

#remove item(property) and all associated properties
#HKLM:\System\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration> rm $server
#HKLM:\System\CurrentControlSet\Services\SNMP\Parameters> rm ValidCommunities

} #if ($tabTraps)

if ($tabSecurity)
{
#create item ValidCommunities
$validCommunities = “$snmpParameters\ValidCommunities”

#create item ($server)
if (-not (Test-Path $validCommunities))
{
New-Item -Name “ValidCommunities” `
-Path $snmpParameters
}

#repeat to add more properties to this item ValidCommunities
#4 = read only
New-ItemProperty -Name communityName `
-Value 4 -Path $validCommunities

#check
#Get-ItemProperty “.\ValidCommunities”

$i = 0
$i = (Get-ItemProperty “$snmpParameters\PermittedManagers” |
Measure).count
$i++

#repeat to add more properties to this item ($server)
New-ItemProperty -Name $i `
-Value $hostFqdn -Path “$snmpParameters\PermittedManagers”

#check
#cd $snmpParameters
#Get-ItemProperty “.\PermittedManagers”

} #if ($tabSecurity)

if ($sysServices)
{
#1 Physical, 4 Internet, 64 Applications, 8 End-to-end
Set-ItemProperty -Name sysServices `
-Value $sysServicesValue `
-Path “$snmpParameters\RFC1156Agent”

} #if ($sysServices)

if ($EnableAuthenticationTraps)
{
#0 disabled
Set-ItemProperty -Name EnableAuthenticationTraps `
-Value $EnableAuthenticationTrapsValue `
-Path “$snmpParameters”
} #if ($EnableAuthenticationTraps)

} #function Set-SNMP()

Leave a Reply