r/PowerShell • u/Kiddo_Ogami • 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
1
u/Kiddo_Ogami Dec 20 '22
Quick update: I found a web site where it's explained how to use a specific runas profile to execute a Unit Monitor:
https://azurecloudai.blog/2019/06/16/add-run-as-profile-to-scom-unit-monitor/
Nevertheless, it seems the monitor continues to be run as Local System:
User is NT AUTHORITY\SYSTEM
:(
1
u/Kiddo_Ogami Dec 22 '22
So in the end it was an issue with the GPO.
Here is how it solved (on every server where the monitor was not working):
After reboot, the monitor started working.
It was a looong journey, and I wish to thank here all the people that tried to help me.
2
u/SMFX Dec 20 '22
Retry, timeout
https://learn.microsoft.com/en-us/windows-hardware/drivers/print/isnmp-open
1
u/astalush Dec 20 '22
I think 2 is the version (1, 2c, 3) and 3000 is the port. « Public » is the community for that 2c , are you sure it’s the same as the one on the distant server? https://www.noction.com/blog/snmp-versions-evolution-security
1
u/dragoncuddler Dec 20 '22
How are you running this within SCOM? Can you share the xml (** out any sensitive information)?
A few options (I can't test this so these are just ideas):
2. Make sure you are manually running it on the same machine that you are using the SCOM agent on. E.g. are PowerShell settings \ modules \ security the same on the machine you manually run it vs the machine where SCOM runs it?
3. Credentials - When you are running the account yourself then you are using your credentials. When you are running it through SCOM, you are using the agent action account credentials (probably local system). Can you change the agent to use a windows account to run the script to see if it is a credentials issue?
Good luck
2
u/PowerShell-Bot Dec 20 '22 edited Dec 20 '22
It appears that you have used inline code formatting when a code block should have been used.
Consider using a code block for longer sequences of code. To correct the formatting, highlight your code then click the ‘Code Block’ button in the editing toolbar.
Beep-boop, I am a bot. | Remove-Item