Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SFTP » 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 can be well integrated into ASP .NET (Visual Basic) environments.

This document describes how Network Component's SNMP objects can be integrated into ASP .NET (Visual Basic) projects.

Network Component is compliant with SNMP v1 and SNMP v2c. Different SNMP data types are supported, including:

The following operations are supported:


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:

$objSnmpManager = new-object -comobject AxNetwork.SnmpManager

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

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

Appendix: Full source code

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

# ********************************************************************
#  Function PrintSnmpObject
# ********************************************************************
Function PrintSnmpObject ($objSnmpObject )
{
   Write-Host "  OID   : " $objSnmpObject.OID
   Write-Host "  Name  : " $objSnmpObject.OIDName
   Write-Host "  Value : " $objSnmpObject.Value
   Write-Host "  Type  : " GetTypeString( $objSnmpObject.Type )
   Write-Host  " "
}

# ********************************************************************
#  Function GetTypeString
# ********************************************************************
Function GetTypeString($lType)
{
   switch($lType)
   {
     $objConstants.nwSNMP_TYPE_INTEGER32{GetTypeString = "nwSNMP_TYPE_INTEGER32"}
     $objConstants.nwSNMP_TYPE_BITS{GetTypeString = "nwSNMP_TYPE_BITS"}
     $objConstants.nwSNMP_TYPE_OCTETSTRING{GetTypeString = "nwSNMP_TYPE_OCTETSTRING"}
     $objConstants.nwSNMP_TYPE_NULL{GetTypeString = "nwSNMP_TYPE_NULL"}
     $objConstants.nwSNMP_TYPE_OBJECTIDENTIFIER{GetTypeString = "nwSNMP_TYPE_OBJECTIDENTIFIER"}
     $objConstants.nwSNMP_TYPE_SEQUENCE{GetTypeString = "nwSNMP_TYPE_SEQUENCE"}
     $objConstants.nwSNMP_TYPE_IPADDRESS{GetTypeString = "nwSNMP_TYPE_IPADDRESS"}
     $objConstants.nwSNMP_TYPE_COUNTER32 {GetTypeString = "nwSNMP_TYPE_COUNTER32"}
     $objConstants.nwSNMP_TYPE_GAUGE32{GetTypeString = "nwSNMP_TYPE_GAUGE32"}
     $objConstants.nwSNMP_TYPE_TIMETICKS{GetTypeString = "nwSNMP_TYPE_TIMETICKS"}
     $objConstants.nwSNMP_TYPE_OPAQUE{GetTypeString = "nwSNMP_TYPE_OPAQUE"}
     $objConstants.nwSNMP_TYPE_COUNTER64{GetTypeString = "nwSNMP_TYPE_COUNTER64"}
     $objConstants.nwSNMP_TYPE_UNSIGNED32{GetTypeString = "nwSNMP_TYPE_UNSIGNED32"}
     default{GetTypeString= "UNKNOWN"}
  }
}

# ***************************************************************************
# 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
$objSnmpManager = new-object -comobject AxNetwork.SnmpManager
$objConstants = new-object -comobject AxNetwork.NwConstants

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

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

# Get Host, community name and optionally a MIB file
$strHostName    = ReadInput "Enter the hostname (a remote or local hostname)" "localhost" $False
$strCommunity   = ReadInput "Enter community" "public" $False 
$strMibFile     = ReadInput "Enter location of MIB file (optional, only required when using alpha-numeric OID's)" "" $True

# Set SNMP protocol version (V1 or V2C)
$objSnmpManager.ProtocolVersion = $objConstants.nwSNMP_VERSION_V2C

# Initialize SNMP
$objSnmpManager.Initialize()
$res = "Initialize, result: " + $objSnmpManager.LastError + " (" + $objSnmpManager.GetErrorDescription( $objSnmpManager.LastError ) + ")"
Write-Host $res
If( $objSnmpManager.LastError -ne 0 )
{
  exit
}

If( $strMibFile -ne "" )
{
  $objSnmpManager.LoadMibFile( $strMibFile )
  $res = "LoadMibFile: " + $objSnmpManager.LastError + " (" + $objSnmpManager.GetErrorDescription( $objSnmpManager.LastError ) + ")"
  Write-Host $res
}

# Open SNMP session. Pass hostname and community. 
# Note: Port 161 is used. To specify  a different port, pass the port number as 3rd parameter (optional) 
$objSnmpManager.Open($strHostName, $strCommunity)
$chr = [char] 34
$res =  "Open( " + $strHostName + ", " + $chr + $strCommunity + $chr + " ), result: " + $objSnmpManager.LastError + " (" + $objSnmpManager.GetErrorDescription($objSnmpManager.LastError) + ")"
Write-Host $res
If( $objSnmpManager.LastError -ne 0 )
{
  $objSnmpManager.Shutdown()
  $res = "Shutdown, result: " + $objSnmpManager.LastError + " (" + $objSnmpManager.GetErrorDescription( $objSnmpManager.LastError ) + ")"  
  Write-Host $res
  exit
}

# Walk
$strOID = "system.sysName.0"
$objSnmpObject = $objSnmpManager.Get( $strOID )
While( $objSnmpManager.LastError -eq 0)
{
  PrintSnmpObject($objSnmpObject)
  trap [Exception] 
  { $objSnmpObject = $objSnmpManager.GetNext();
    continue; 
  }
}

# Close
$objSnmpManager.Close()
$res = "Close, result: " + $objSnmpManager.LastError + " (" + $objSnmpManager.GetErrorDescription( $objSnmpManager.LastError ) + ")"
Write-Host $res

# Shutdown SNMP
$objSnmpManager.Shutdown()
$res =  "Shutdown(): " + $objSnmpManager.LastError + " (" + $objSnmpManager.GetErrorDescription( $objSnmpManager.LastError ) + ")"
Write-Host $res

# 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.