The below PowerShell script will check if SNMP is installed, if it is not installed it will install the service and configure it.

#!powershell

# Setup Key Values
###################################
## Security Tab
$managers = @(“localhost”,”mgt1.knowledgeascent.com”)
$ReadOnlyCommunities = @(“public”)
$ReadWriteCommunities = @(“publicrw”)

## Agent Tab
$sysContact = “[email protected]
$sysLocation = “KA Data Center”
$sysServices = “79”

## Traps Tab
$communityname = “public”
$trapdestination = @(“localhost”)

## Additional Variables
$server = $env:computername

##############################################
# Installing SNMP Services #
##############################################

Import-Module ServerManager
write-host ” ” -ForegroundColor Green
Write-host “Enabling ServerManager to check SNMP” -ForegroundColor Green
write-host “=====================================” -ForegroundColor Green

# Check if SNMP-Service is already installed
write-host ” ” -ForegroundColor Green
Write-host “Checking to see if SNMP service is installed…” -ForegroundColor Green
$SNMPCheck = Get-WindowsFeature -Name SNMP-Service

#
If ($SNMPCheck.Installed -ne “True”)
{
#Install/Enable SNMP-Service
Write-host “SNMP is NOT installed…”
Write-Host “SNMP Service Installing…”
Get-WindowsFeature -name SNMP* | Add-WindowsFeature -IncludeAllSubFeature | Out-Null
}
Else
{
Write-Host “SNMP Services already Installed” -ForegroundColor Green
}

if ($SNMPCheck.Installed -eq “True”)
{
Write-Host “SNMP installed on” $server”, proceeding with configuration…” -ForegroundColor Green
$objReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’, $server)

###################################################
Write-Host “Security Tab” -ForegroundColor Yellow
###################################################
Write-Host “Accept SNMP packets from these hosts:” -ForegroundColor Yellow
$Values_counter = 1
$objRegKeyPM = $objReg.OpenSubKey(“SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\PermittedManagers”,$true)

Foreach ($managers in $managers)
{
$RegValue = $objRegKeyPM.GetValue($Values_counter.ToString())
if ($RegValue -eq $managers)
{
Write-Host “Skipping adding this host name ” $managers “as it is already configured” -ForegroundColor White
}
else
{
Write-Host “Adding” $Values_counter $Managers “to HKLM\..\SNMP\Parameters\PermittedManagers”-ForegroundColor Green
$objRegKeyPM.SetValue($Values_counter.ToString(), $managers)
}
$devnull = $objRegKeyPM.Close
$Values_counter++
}

Write-Host “Accepted Community Names:” -ForegroundColor Yellow
Write-Host “Accepted Read Only Community Names:” -ForegroundColor Yellow
$objRegKeyVCRO = $objReg.OpenSubKey(“SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ValidCommunities”,$true)

Foreach ($ReadOnlyCommunities in $ReadOnlyCommunities)
{
$RegValueRO = $objRegKeyVCRO.GetValueNames()
if ($RegValueRO -eq $ReadOnlyCommunities)
{
Write-Host “Skipping adding this community name ” $ReadOnlyCommunities “as it is already configured” -ForegroundColor White
}
else
{
Write-Host “Adding Read Only Community” $ReadOnlyCommunities “to HKLM\..\SNMP\Parameters\ValidCommunities”-ForegroundColor Green
$objRegKeyVCRO.SetValue($ReadOnlyCommunities, 4)
}
$devnull = $objRegKeyVCRO.Close
}

Write-Host “Accepted Read Write Community Names:” -ForegroundColor Yellow
$objRegKeyVCRW = $objReg.OpenSubKey(“SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ValidCommunities”,$true)
$Duplicate = $false

Foreach ($ReadWriteCommunities in $ReadWriteCommunities)
{
$RegValueRW = $objRegKeyVCRW.GetValueNames()
if ($RegValueRW -eq $ReadWriteCommunities)
{
Write-Host “Skipping adding this community name ” $ReadWriteCommunities “as it is already configured” -ForegroundColor White
}
else
{
Write-Host “Adding Read Write Community” $ReadWriteCommunities “to HKLM\..\SNMP\Parameters\ValidCommunities”-ForegroundColor Green
$objRegKeyVCRW.SetValue($ReadWriteCommunities, 8)
}
$devnull = $objRegKeyVCRW.Close
}

###################################################
Write-Host “Agent Tab” -ForegroundColor Yellow
###################################################

Write-Host “Adding Contact Name” $sysContact “to HKLM\..\SNMP\Parameters\RFC1156Agent”-ForegroundColor Green
reg add “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent” /v sysContact /t REG_SZ /d $sysContact /f | Out-Null

Write-Host “Adding Location” $sysLocation “to HKLM\..\SNMP\Parameters\RFC1156Agent”-ForegroundColor Green
reg add “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent” /v sysLocation /t REG_SZ /d $sysLocation /f | Out-Null

Write-Host “Adding requires services code to monitor ” $sysServices “to HKLM\..\SNMP\Parameters\RFC1156Agent”-ForegroundColor Green
reg add “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent” /v sysServices /t REG_DWORD /d 79 /f | Out-Null

###################################################
Write-Host “Traps Tab” -ForegroundColor Yellow
###################################################
Write-Host “Community Name” -ForegroundColor Yellow
$Values_counter = 1
Write-Host “Creating Subkey TrapConfiguration”
$TrapKey = $objReg.OpenSubKey(“SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters”,$true)
$devnull = $TrapKey.CreateSubKey(“TrapConfiguration”)

Write-Host “Creating Subkey” $communityname
$TrapKey1 = $objReg.OpenSubKey(“SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\TrapConfiguration”,$true)
$devnull = $TrapKey1.CreateSubKey($communityname)
$devnull = $TrapKey.Close

Write-Host “Trap destinations:” -ForegroundColor Yellow
$TrapKey2 = “SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\TrapConfiguration\\”
$TrapKey2WCommunity = $TrapKey2 + $communityname
$FinalTrapKey = $objReg.OpenSubKey($TrapKey2WCommunity,$true)

Foreach ($trapdestination in $trapdestination)
{
$TrapvalueDest = $FinalTrapKey.GetValue($Values_counter).ToString()
if ($TrapvalueDest -eq $trapdestination)
{

Write-Host “Skipping adding this trap destination ” $trapdestination “as it is already configured” -ForegroundColor White
}
else
{
Write-Host “Adding” $Values_counter $trapdestination “to HKLM\..\SNMP\Parameters\TrapConfiguration”-ForegroundColor Green
$FinalTrapKey.SetValue($Values_counter.ToString(), $trapdestination)
}
$devnull = $FinalTrapKey.Close
$Values_counter++
}

}
Else
{
Write-Host “Script Error, please check the script again” -ForegroundColor RED
}