WMI to access devices over SNMP – Error Invalid Namespace

January 20, 2015

I wanted to run an old script of mine to read out some network printer information using WMI – SNMP.

But I got an error saying that “Invalid Namespace“ Sad smile

Solutions :

You need to add the SNMP WMI providers to your machine.

1. Go to the Control Panel
2. Next select Programs and Features
3. Select Simple Network Management Protocol (SNMP)
4. Select WMI SNMP Provider

image

Once installed all errors are gone Smile

; Initialize error handler
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")   $iIP = 31   For $iIP = 31 To 45 ; IP range   $strTargetSnmpDevice = "10.0.0." & $iIP
 ConsoleWrite("Target Device : " & $strTargetSnmpDevice & @CRLF)
 ConsoleWrite("-------------" & @CRLF)   $objWmiLocator = ObjCreate("WbemScripting.SWbemLocator")
 $objWmiServices = $objWmiLocator.ConnectServer("", "root\snmp\localhost")   $objWmiNamedValueSet = ObjCreate("WbemScripting.SWbemNamedValueSet")
 $objWmiNamedValueSet.Add ("AgentAddress", $strTargetSnmpDevice)
 $objWmiNamedValueSet.Add ("AgentReadCommunityName", "public")   $colIfTable = $objWmiServices.InstancesOf("SNMP_RFC1213_MIB_ifTable",Default , $objWmiNamedValueSet)   For $objInterface In $colIfTable
    ConsoleWrite ("ifIndex [Key]:        " & $objInterface.ifIndex  & @CRLF & _
        "   ifAdminStatus:     " & $objInterface.ifAdminStatus 		& @CRLF & _
        "   ifDescr:           " & $objInterface.ifDescr         	& @CRLF & _
        "   ifInDiscards:      " & $objInterface.ifInDiscards    	& @CRLF & _
        "   ifInErrors:        " & $objInterface.ifInErrors      	& @CRLF & _
        "   ifInNUcastPkts:    " & $objInterface.ifInNUcastPkts   	& @CRLF & _
        "   ifInOctets:        " & $objInterface.ifInOctets      	& @CRLF & _
        "   ifInUcastPkts:     " & $objInterface.ifInUcastPkts 		& @CRLF & _
        "   ifInUnknownProtos: " & $objInterface.ifInUnknownProtos 	& @CRLF & _
        "   ifLastChange:      " & $objInterface.ifLastChange     	& @CRLF & _
        "   ifMtu:             " & $objInterface.ifMtu            	& @CRLF & _
        "   ifOperStatus:      " & $objInterface.ifOperStatus     	& @CRLF & _
        "   ifOutDiscards:     " & $objInterface.ifOutDiscards 		& @CRLF & _
        "   ifOutErrors:       " & $objInterface.ifOutErrors      	& @CRLF & _
        "   ifOutNUcastPkts:   " & $objInterface.ifOutNUcastPkts  	& @CRLF & _
        "   ifOutOctets:       " & $objInterface.ifOutOctets      	& @CRLF & _
        "   ifOutQLen:         " & $objInterface.ifOutQLen        	& @CRLF & _
        "   ifOutUcastPkts:    " & $objInterface.ifOutUcastPkts   	& @CRLF & _
        "   ifPhysAddress:     " & $objInterface.ifPhysAddress 		& @CRLF & _
        "   ifSpecific:        " & $objInterface.ifSpecific       	& @CRLF & _
        "   ifSpeed:           " & $objInterface.ifSpeed          	& @CRLF & _
        "   ifType:            " & $objInterface.ifType           	& @CRLF)
		ConsoleWrite("" & @CRLF)
 Next
ConsoleWrite("" & @CRLF)
Next

Output is like this :

image

For Powershell it looks a bit different:


CLS

For ($iIP=32; $iIP -le 45; $iIP++){

    $CommunityString = "public"
    $IPAddress     = "10.0.0."+$iIP
    write-host $IPAddress

    $Opt = new-object Management.ConnectionOptions
    $Opt.Impersonation = 'Impersonate'

    $context = new-object management.ManagementNamedValueCollection
    $context.Add("AgentAddress", $IPAddress)
    $context.Add("AgentReadCommunityName", $CommunityString)

    $ogo =  new-object management.ObjectGetOptions($context, (new-object TimeSpan(0,0,0,5)), $true)

    $scope = new-object management.ManagementScope("root\snmp\localhost", $Opt)

    $query = new-object Management.ObjectQuery("select * from SNMP_RFC1213_MIB_system")

    $searcher = new-object Management.ManagementObjectSearcher($scope,$query)
    $searcher.Options.Context = $context

    $searcher.get()

}

Output is like this :

image

If you want to access devices like routers, switches, printers etc. using Native SNMP protocol.

You will need some tools to get hold of their public or private MIB Object ID’s.

Once of the many tools out there is Softperfect Network scanner.

image

Maybe I will address this later how to access the network Devices using native SNMP protocol Winking smile

Enjoy !