r/PowerShell • • Dec 20 '22

Solved SNMP query

Hi all, I am trying to use SNMP to query status of a UPS using the code in a #SCOM monitor.

If I run the code from PS ISE it works fine, but when it's run by the SCOM agent, I have this error:

Failed to open SNMP session

This is the code I use:

try

{

$SNMP = New-Object -ComObject olePrn.OleSNMP

}

catch

{

Write-Debug "Error creating SNMP object"

"Error creating SNMP object - $($_.Exception.Message)" | out-file -FilePath "C:\temp\upsonbatt.log" -append

exit

}

try

{

$UPSIPAddress = [System.Net.Dns]::GetHostAddresses($UpsName).IPAddressToString

$SNMP.open($UPSIPAddress, "public", 2, 3000)

}

catch

{

Write-Debug "Error opening SNMP connection"

"SNMPERR opening $UpsName with IP $UPSIPAddress - $($_.Exception.Message)" | out-file -FilePath "C:\temp\upsonbatt.log" -append

exit

}

Someone knows what the last 2 parameters in this command mean ?

$SNMP.open($UPSIPAddress, "public", 2, 3000)

I mean the 2 and the 3000

I cannot find anything on the web, or maybe I cannot find what to look for ;)

Thanks in advance

4 Upvotes

32 comments sorted by

View all comments

Show parent comment

1

u/Kiddo_Ogami Dec 21 '22

I tried but had this error:

The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))At line:14 char:1+ $SCOMapi.Return($Propertybag)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : OperationStopped: (:) [], COMException    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

The object $SCOMapi was created successfully however :O