There are a few different ways to query SNMP on devices. I will share 3 ways that I have found to be pretty accurate and quick.
Before we begin here are 3 utilities you will need to perform the queries.
1 utility is an executable the other 2 are DLL files that you will be loading into your powershell session.
SNMPGet.exe Tool
The latest release of “SharpSnmpLib.dll” can be downloaded from:
The latest release of “SnmpSharpNet.dll” can be downloaded from:
===========================================
Using SNMPGet.exe
CLS
#Path to SNMPGet.exe
$SNMPGet = "C:\Users\James\Desktop\SNMP\SnmpGet.exe"
$CommunityString = "public"
$OID = ".1.3.6.1.2.1.1.1.0"
$IP = "123.123.123.123"
Try
{
if(($(& $SNMPGet -r:$IP -c:$CommunityString -o:$OID -t:1) | Select-Object -Skip 3) -notlike "*Failed*")
{
$True
}
}
Catch
{}
Using SnmpSharpNet.dll
CLS
#Path to SnmpSharpNet.dll
$DLL = "C:\Users\James\Desktop\SNMP\8-5\SnmpSharpNet.dll"
Add-Type -Path $DLL
$CommunityString = "public"
$OID = ".1.3.6.1.2.1.1.1.0"
$IP = "123.123.123.123"
$SNMP = new-object SnmpSharpNet.SimpleSnmp($IP,$CommunityString)
$SNMP.Timeout = 100
$Version = "Ver1"
Try
{
if($NULL -ne ($SNMP.Get([SnmpSharpNet.SnmpVersion]::$Version,$OID)).Values)
{
$True
}
}
Catch
{}
Using SharpSnmpLib.dll
CLS
#Path to SnmpSharpNet.dll
$DLL = "C:\Users\James\Desktop\SNMP\8-5\SharpSnmpLib.dll"
Add-Type -Path $DLL
function Get-SnmpData
{
[CmdletBinding()]
param (
# Endpoint IP address.
[Parameter(
Mandatory = $true,
HelpMessage = 'Endpoint IP address'
)]
[Net.IPAddress]$IP,
# OID list.
[Parameter(
Mandatory = $true,
HelpMessage = 'OID list'
)]
[string[]]$OID,
# SNMP Community.
[string]$Community = 'public',
# SNMP port.
[int]$UDPport = 161,
# SNMP version.
[Lextm.SharpSnmpLib.VersionCode]$Version = 'V2',
# Time out value.
[int]$TimeOut = 3000
)
$variableList = New-Object Collections.Generic.List[Lextm.SharpSnmpLib.Variable]
foreach ($singleOID in $OID)
{
$variableList.Add($(
New-Object Lextm.SharpSnmpLib.ObjectIdentifier $singleOID
))
}
$endpoint = New-Object Net.IpEndPoint $IP, $UDPport
try
{
$message = [Lextm.SharpSnmpLib.Messaging.Messenger]::Get(
$Version,
$endpoint,
$Community,
$variableList,
$TimeOut
)
} catch
{
Write-Warning "SNMP Get error: $_"
return
}
foreach ($variable in $message)
{
New-Object PSObject -Property @{
OID = $variable.Id.ToString()
Data = $variable.Data.ToString()
}
}
}
$CommunityString = "public"
$OID = ".1.3.6.1.2.1.1.1.0"
$IP = "123.123.123.123"
$SNMP = new-object SnmpSharpNet.SimpleSnmp($IP,$CommunityString)
$SNMP.Timeout = 100
$Version = "Ver1"
Try
{
if($NULL -ne ((Get-SnmpData -IP $IP -Community $CommunityString -Version V1 -TimeOut 100 -OID $OID -WarningAction SilentlyContinue).Data))
{
$True
}
}
Catch
{}
In terms of Performance if you’re running this against multiple devices the speed is fastest with the script utilizing “SnmpSharpNet.dll”.