Difference between revisions of "SNMP and PowerShell"

Jump to navigation Jump to search
1,422 bytes added ,  10:43, 10 October 2016
→‎Overview: Updated for v8 of library
(Added "Snap-In for PowerShell")
(→‎Overview: Updated for v8 of library)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{#widget:Widget:GoogleAdSenseSkyscraper}}
== Overview ==
== Overview ==
This page covers using PowerShell as an SNMP client, so that you can poll/probe/interrogate SNMP servers (be they servers, network devices, UPS's, etc).
This page covers using PowerShell as an SNMP client, so that you can poll/probe/interrogate SNMP servers (be they servers, network devices, UPS's, etc).
Line 7: Line 8:
You don't need to use /n's [http://www.powershellinside.com/powershell/netcmdlets/ NetCmdlets], or run the [http://www.net-snmp.org/ Net-SNMP] executables, as other examples on the web do.
You don't need to use /n's [http://www.powershellinside.com/powershell/netcmdlets/ NetCmdlets], or run the [http://www.net-snmp.org/ Net-SNMP] executables, as other examples on the web do.


In order to make the library available for PowerShell to use...
=== Installation ===
# Download the latest '''BigDipper''' binary from http://sharpsnmplib.codeplex.com/releases/view/78947 and extract
In order to make the library available for PowerShell to use you need to have the .NET dll library file available for Powershell
# Go to http://sharpsnmplib.codeplex.com and browse to the '''Downloads''' tab
#* Current latest is v8 (TritonMate), later versions are available from [https://www.nuget.org Nuget]
# Download the latest binary (eg tritonmate_8.0_bin.zip from http://sharpsnmplib.codeplex.com/releases/view/79079) and extract
# Copy the <code>SharpSnmpLib.dll</code> file to somewhere you can find it
# Copy the <code>SharpSnmpLib.dll</code> file to somewhere you can find it
#* EG in a folder <code>/lib/LexTmSharpSNMP/</code> under where you run your scripts from
#* EG in a folder <code>/lib/LexTmSharpSNMP/</code> under where you run your scripts from
#* Note that you may need to Unblock the file (right-click and select '''Properties''' and look for '''Unblock''' towards bottom of Properties window)


'''Windows 2008 Server doesn't have .NET 3.5 or 4 installed by default''', therefore it needs to be installed.  To do so...
'''Windows 2008 Server doesn't have .NET 3.5 or 4 installed by default''', therefore it needs to be installed.  To do so...
Line 26: Line 31:


=== Generic Objects ===
=== Generic Objects ===
In order to use the library you need to be able to create generic objects, which doesn't work in PowerShell v1.  Its meant to be fixed in v2, but I've not got it to work (probably because I don't actually understand what they are really. So I've used this [http://www.leeholmes.com/blog/2006/08/18/creating-generic-types-in-powershell/ workaround by Lee Holmes].
==== PowerShell v1 and v2 ====
In order to use the library you need to be able to create generic objects, which doesn't work in PowerShell v1.  Its meant to be fixed in v2, but I've not got it to work (probably because I don't actually understand what they are really. So I've used this [http://www.leeholmes.com/blog/2006/08/18/creating-generic-types-in-powershell/ workaround by Lee Holmes]).


The function below is Lee Holmes' work wrapped into a function...
The function below is Lee Holmes' work wrapped into a function...
Line 60: Line 66:
}
}
</source>
</source>
==== PowerShell v3 ====
Generic Objects do work in PowerShell v3!  So you don't need to use the <code>New-GenericObject</code> function provided above.  It's still used in the examples below so that they're backwards compatible, but I've also added a commented out line which you can use instead, should you be running v3.


== SNMP Get ==
== SNMP Get ==
Line 69: Line 78:


     # Create OID variable list
     # Create OID variable list
     $vList = New-GenericObject System.Collections.Generic.List Lextm.SharpSnmpLib.Variable
     $vList = New-GenericObject System.Collections.Generic.List Lextm.SharpSnmpLib.Variable                       # PowerShell v1 and v2
    # $vList = New-Object 'System.Collections.Generic.List[Lextm.SharpSnmpLib.Variable]'                          # PowerShell v3
     foreach ($sOID in $sOIDs) {
     foreach ($sOID in $sOIDs) {
         $oid = New-Object Lextm.SharpSnmpLib.ObjectIdentifier ($sOID)
         $oid = New-Object Lextm.SharpSnmpLib.ObjectIdentifier ($sOID)
Line 113: Line 123:
      
      
     # Create list for results
     # Create list for results
     $results = New-GenericObject System.Collections.Generic.List Lextm.SharpSnmpLib.Variable
     $results = New-GenericObject System.Collections.Generic.List Lextm.SharpSnmpLib.Variable                       # PowerShell v1 and v2
    # $results = New-Object 'System.Collections.Generic.List[Lextm.SharpSnmpLib.Variable]'                        # PowerShell v3
      
      
     # Create endpoint for SNMP server
     # Create endpoint for SNMP server
Line 144: Line 155:


== Example Usage ==
== Example Usage ==
{| class="vwiki-boxout"
|-
| '''For further examples see - http://vblog.strutt.org.uk/tag/sharpsnmplib/'''
|}
If you saved the [[#Generic_Objects|Generic Objects]] workaround script, the SNMP DLL file, and the above functions in <code>lib</code> sub-folder to where your running your PowerShell from, then you could perform queries as follows...
If you saved the [[#Generic_Objects|Generic Objects]] workaround script, the SNMP DLL file, and the above functions in <code>lib</code> sub-folder to where your running your PowerShell from, then you could perform queries as follows...


Line 191: Line 208:
# Download the file below to a <code>lib</code> sub-folder, underneath where you normally run your scripts from
# Download the file below to a <code>lib</code> sub-folder, underneath where you normally run your scripts from
#* http://dl.sandfordit.com/scripts/SNMP.ps1
#* http://dl.sandfordit.com/scripts/SNMP.ps1
#* You may need to unblock the downloaded file, right-click over the file and select '''Properties''', then click '''Unblock'''
# Download the latest '''BigDipper''' binary from http://sharpsnmplib.codeplex.com/releases/view/78947 and extract
# Download the latest '''BigDipper''' binary from http://sharpsnmplib.codeplex.com/releases/view/78947 and extract
# Copy the <code>SharpSnmpLib.dll</code> file to a <code>LexTmSharpSNMP/</code> folder, underneath the <code>lib</code> folder
# Copy the <code>SharpSnmpLib.dll</code> file to a <code>LexTmSharpSNMP/</code> folder, underneath the <code>lib</code> folder

Navigation menu