Difference between revisions of "SNMP and PowerShell"

Jump to navigation Jump to search
Minor typo fixes and PowerShell v3 Generic Objects compatability
(Added "Snap-In for PowerShell")
(Minor typo fixes and PowerShell v3 Generic Objects compatability)
Line 8: Line 8:


In order to make the library available for PowerShell to use...
In order to make the library available for PowerShell to use...
# Download the latest '''BigDipper''' binary from http://sharpsnmplib.codeplex.com/releases/view/78947 and extract
# Download the latest '''BigDipper''' binary (eg bigdipper_7.5_bin.zip) from http://sharpsnmplib.codeplex.com/releases/view/78947 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
Line 26: Line 26:


=== 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 61:
}
}
</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 73:


     # 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 118:
      
      
     # 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 191: Line 197:
# 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