The greatest challenge to any thinker is stating the problem in a way that will allow a solution

Bertrand Russell

By

On 12 Feb 2013

PowerShell

Tags: , ,


This post provides an example of how to gather some basic system info from an SNMP enabled host using PowerShell.

Why wouldn’t I use WMI?

You could in many circumstances.  In general the amount of information you can get from WMI is far greater than what’s available via SNMP, however,

  • In order to access WMI you often end up requiring full admin rights on the target systems, which is a security nightmare.
  • WMI is Windows platform dependant, whereas SNMP is normally available for any enterprise device.

PowerShell and SNMP

In order to be able to use PowerShell as an SNMP client you need to use a free 3rd party .NET library.  It wasn’t specifically created with PowerShell in mind, but as its .NET, this isn’t a problem.  There are of course paid for PowerShell SNMP snap-ins (but why would you pay for something you can legally get for free?).

For further info on how to get yourself set-up, see my Wiki page – http://vwiki.co.uk/SNMP_and_PowerShell

In order to run the script below you need to have the SharpSnmpLib.dll file, so that PowerShell can run SNMP operations.  Plus the script functions from the Wiki page in a file called SNMP.ps1, these act as a wrapper for the DLL.  See http://vwiki.co.uk/SNMP_and_PowerShell#Snap-In_for_PowerShell for futher info.

 The Script

$SNMPhost = "192.168.10.123"     # IP address of host to query
 
. .\lib\SNMP.ps1                 # Include SNMP helper functions (see above)
 
# Load SNMP assembly (update path to DLL as required)
[reflection.assembly]::LoadFrom( (Resolve-Path ".\lib\LexTmSharpSNMP\SharpSnmpLib.dll") )
 
# Basic queries
Write-Host ("Hostname   : " + (Invoke-SnmpGet $SNMPhost .1.3.6.1.2.1.1.5.0).Data)
Write-Host ("Description: " + (Invoke-SnmpGet $SNMPhost .1.3.6.1.2.1.1.1.0).Data)
Write-Host ("System     : " + (Invoke-SnmpGet $SNMPhost .1.3.6.1.2.1.1.2.0).Data)
Write-Host ("Memory (MB): " + [math]::Round(((Invoke-SnmpGet $SNMPhost .1.3.6.1.2.1.25.2.2.0).Data)/1024, 0) )
 
# Walk whole of MIB2 Device Table
$devices = Invoke-SnmpWalk $SNMPhost .1.3.6.1.2.1.25.3.2
 
#Extract number of processors and disks
Write-Host ("Processors : " + ($devices |?{$_.Data -eq ".1.3.6.1.2.1.25.3.1.3"} | Select -ExpandProperty Data).count)
Write-Host ("Disks      : " + ($devices |?{$_.Data -eq ".1.3.6.1.2.1.25.3.1.6"} | Select -ExpandProperty Data).count)
 
# Get index ID's of disks, and get some data for each
$diskIDs = $devices |?{$_.Data -eq ".1.3.6.1.2.1.25.3.1.6"} | Select -ExpandProperty OID | % {$_ -Replace ".1.3.6.1.2.1.25.3.2.1.2.", ""}
$disklist = ""
foreach ($diskID in $diskIDs) {
$disklist += (Invoke-SnmpGet $SNMPhost .1.3.6.1.2.1.25.3.2.1.3.$diskID).Data + "  "
}
Write-Host ("Disks      : " + $disklist)

Example Output

From a Windows system expect something like…

Hostname   : TEST-SVR
Description: Hardware: x86 Family 16 Model 2 Stepping 3 AT/AT COMPATIBLE - Software: Windows Version 5.2 (Build 3790 Multiprocessor Free)
System     : .1.3.6.1.4.1.311.1.1.3.1.2
Memory (MB): 1023
Processors : 1
Disks      : 3
Disks      : A:\ C:\ D:\

and from a Linux system something like…

Hostname   : sys1
Description: Linux 2.7.3
System     : .1.3.6.1.4.1.12345
Memory (MB): 250
Processors : 1
Disks      : 3
Disks      : SCSI disk (/dev/sda)  SCSI disk (/dev/sdb)  SCSI disk (/dev/sdc)

 


Leave a Reply

XHTML: You can use these tags if you know what they are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.