Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SNMP MIB » Powershell

How to use SNMP in Powershell

Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.

Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Network Component can be well integrated into any development platform that supports ActiveX objects.


SNMP MIB Browsing can be well integrated into ASP .NET environments. This document describes how Network Component's SNMP MIB Browser can be integrated into ASP .NET projects.

A management information base (MIB) is a database used to manage the devices in a communications network. The database is hierarchical (tree-structured) and entries are addressed through object identifiers (OID's). A MIB should contain information on these commands and on the target objects (controllable entities or potential sources of status information) with a view to tuning the network transport to the current needs. Each type of object in a MIB database has a name, a syntax, and an encoding. The name is represented uniquely as an OID. An OID is an administratively assigned name. The administrative policies used for assigning names are discussed later in this memo.

Use Network Component's 'SnmpMibBrowser' object to load a MIB database into memory and iterate over all objects and view all properties.


Step 1: Download and install the Network Component

Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new script

Create a new script using your favorite editor. You can simply use notepad. However, a Powershell editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced script to communicate using the Network Component.

Step 3: Create the Network Component object in Powershell

Create a new Powershell file called DEMO.PS1.

Create the Network Component object(s) like this:

$objSnmpMib = new-object -comobject AxNetwork.SnmpMib

Now, add the following lines to the file to have your first Network Component Powershell program:

Write-Host "Network Component Version " $objSnmpMib.Version
Write-Host "  Build  " $objSnmpMib.Build 
Write-Host "  Module " $objSnmpMib.Module
Write-Host "License Status: " $objSnmpMib.LicenseStatus

Appendix: Full source code

# *******************************************************************
# ActiveXperts Network Component Sample - SNMP Walk
# Written by ActiveXperts Software - https://www.activexperts.com
# ********************************************************************

# ***************************************************************************
# Function ShowMIBTree
# ***************************************************************************
function ShowMIBTree($objSnmpMib, $objConstants)
{
# Create SnmpManager and NwConstants instances
$objSnmpMib = new-object -comobject AxNetwork.SnmpMibBrowser
$objConstants = new-object -comobject AxNetwork.NwConstants
  
   # Start with "iso"
  $objSnmp = $objSnmpMIB.Get("iso")
   While ($objSnmpMIB.LastError -eq 0)
   {
    $strDescription = $objSnmp.Description -ReplaceEx("`r`n" , " [nl] ")
    $strDescription = $strDescription -ReplaceEx(" [nl] ")
    $nDescription = $strDescription.Length
    if ($nDescription -gt 40)
    {
      $strDescription = $strDescription.substring(0,40)
      $strDescription = $strDescription + "..."
    }
    if ($objSnmp.Access -eq $objConstants.nwMIB_ACCESS_NOACCESS)
    {
      $strAccess = "NOACCESS"
    }
    elseif($objSnmp.Access -eq $objConstants.nwMIB_ACCESS_NOTIFY)
    {
      $strAccess = "NOTIFY"
    }
    elseif($bjSnmp.Access -eq $objConstants.nwMIB_ACCESS_READONLY)
    {
      $strAccess = "READONLY"
    }
    elseif($objSnmp.Access -eq $objConstants.nwMIB_ACCESS_READONLY)
    {
      $strAccess = "READONLY"
    }
    elseif($objSnmp.Access -eq $objConstants.nwMIB_ACCESS_WRITEONLY)
    {
      $strAccess = "WRITEONLY"
    }
    elseif($objSnmp.Access -eq $objConstants.nwMIB_ACCESS_READWRITE)
    {
      $strAccess = "READWRITE"
    }
    elseif($objSnmp.Access -eq $objConstants.nwMIB_ACCESS_READCREATE)
    {
      $strAccess = "READCREATE"
    }
    else
    {
      $strAccess = "n/a"
    }
    
    if($objSnmp.Status -eq $objConstants.nwMIB_STATUS_CURRENT)
    {
     $strStatus = "CURRENT"
    }
    elseif($objSnmp.Status -eq $objConstants.nwMIB_STATUS_DEPRECATED)
    {
     $strStatus = "DEPRECATED"
    }
    elseif($objSnmp.Status = $objConstants.nwMIB_STATUS_OBSOLETE)
    {
     $strStatus = "OBSOLETE"
    }
    elseif($objSnmp.Status -eq $objConstants.nwMIB_STATUS_MANDATORY)
    {
      $strStatus = "MANDATORY"
    }
    else
    {
      $strStatus = "n/a"
    }
    Write-Host "OID: "$objSnmp.OID
    Write-Host "  Name: "$objSnmp.OIDName        
    Write-Host "  Description: "$strDescription
    Write-Host "  Syntax: "$objSnmp.Syntax
    Write-Host "  Access: "$strAccess
    Write-Host "  Status: "$strStatus
    $objSnmp = $objSnmpMIB.GetNext()
  }
}

# ***************************************************************************
# Function ReadInput
# ***************************************************************************
Function ReadInput($strPrompt, $strDefaultValue, $bAllowEmpty)
{ 
  $strReturn = ""  
  If ($strDefaultValue -ne "")
  {
     $strPrompt += " (leave empty for " + $strDefaultValue + "): "
  }
  Do 
  {       
    Write-Host $strPrompt
    $strReturn = read-host
    
    If ($strReturn -eq "" -and $strDefaultValue -ne "")
    {
      $strReturn = $strDefaultValue
      Write-Host $strReturn
    }
    elseif ($strReturn -eq "" -and $bAllowEmpty -eq $True)
    {
      break
    }   
  } While ($strReturn -eq "") 
  Write-Host ""
  return $strReturn
}


# ***************************************************************************
# MAIN SCRIPT
# ***************************************************************************

cls 

# Create SnmpManager and NwConstants instances
$objSnmpMib = new-object -comobject AxNetwork.SnmpMibBrowser
$objConstants = new-object -comobject AxNetwork.NwConstants

# Display ActiveXperts Network Component Version
Write-Host "ActiveXperts Network Component " $objSnmpMib.Version "`nBuild: " $objSnmpMib.Build "`nModule: "  $objSnmpMib.Module "`nLicense Status: " $objSnmpMib.LicenseStatus "`nLicense Key: " $objSnmpMib.LicenseKey "`n`n";

# Logfile
$objSnmpMib.Logfile = $env:temp + "\SnmpMib.log"
Write-Host "Log file used: " $objSnmpMib.Logfile "`n"

$strMibFile = ReadInput "Enter MIB file (leave blank for a Windows System MIB file)" "c:\windows\system32\mib_ii.mib" False 

# LoadMibFile
$objSnmpMib.LoadMibFile($strMibFile)
$res = "LoadMibFile, result: " + $objSnmpMib.LastError + " (" + $objSnmpMib.GetErrorDescription( $objSnmpMib.LastError ) + ")"
Write-Host $res
If($objSnmpMib.LastError -eq 0)
{
   ShowMIBTree $objSnmpMib $objConstants 
}

# Finished
Write-Host "Finished."

To run the code, start Powershell and browse to the location of the file you just created. Enter .\Demo.ps1 to run the code. Notice that if the script is not working, you have to change the execution policy; you can do that with the following command:

Set-ExecutionPolicy -unrestricted

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.