4

i'm using net-snmp extension features to be able to run a powershell script when i query a specific SNMP oid.

snmpd is configured to run a get-storageinfo.ps1 script with some parameters.

the script is being invoked like this by the net-snmp agent:

& c:\scripting\get-storageinfo.ps1 -name somedevicename -detaillevel 2 -oid oidstring

however, things break when i add parameter attributes or CmdletBinding (or both) to my parameter definitions in my get-storageinfo.ps1 script. I don't understand why. I have this at the very top of my script (after some comments actually).

[CmdletBinding()]
Param(
[string]$name,
[string]$detaillevel
[string]$oid
)

or this, same problem

Param(
[Parameter(Mandatory=$True)][string]$name,
[string]$detaillevel
[string]$oid
)

This breaks my snmpd functionality somehow. When the configured OID is being read, i get: "No Such Instance currently exists at this OID"

The following (and only this) works perfectly, without CmdletBinding and parameter attributes at all:

Param(
[string]$name,
[string]$detaillevel
[string]$oid
)

This returns values back to net-snmp just the way it's supposed to do. Net-SNMP (snmpd) is perfectly fine with running the script and returning values when the script is used without the attributes / cmdletbinding. Because of this, i know the arguments are being passed properly by the calling program (snmpd). Problem has to be specific to the attributes or cmdletbinding.

What could be a possible difference between these two declarations regarding the output to a external program like net-snmp? i can't figure out the difference.

UPDATE I've reverted back to the "extend" command of snmpd.conf istead of the "pass" commando. The pass command had no consistent results. I guess i don't understand its usage that well. The extend command has no problems as described earlier. It's a bit weird still, but i'm going forward with "extend".

JayST
  • 61
  • 3
  • Can you give the link where you download net-snmp extension features, and net-snmp Win32 version ? – JPBlanc Mar 05 '14 at 04:38
  • [From this URL](http://sourceforge.net/projects/net-snmp/files/net-snmp%20binaries/5.7-binaries/) i've downloaded the net-snmp-5.7.0-1.x86.exe package for installing Net-SNMP – JayST Mar 05 '14 at 08:13
  • $name contains the OID ? Which version of PowerShell is installed on the computer running snmpd ? Do you take care of the fact that a 32 bit service starts a 32 bit PowerShell version ? – JPBlanc Mar 05 '14 at 08:52
  • no, $name contains just a hostname of some device. I edited the question. i've added the $oid parameter which i also need to pass to be able to return it to the agent, as required by net-snmp. I need to return the oid, value type(integer or string) and the value. Powershell v3 is installed on the machine running snmpd, as shown by get-host output. I've confirmed snmpd starting 32bit version of powershell, taskmanager shows this 32bit powershell process starts running when i snmpget the oid. – JayST Mar 05 '14 at 09:24

1 Answers1

0

It is probably just a posting typo, but you are missing a comma separating the 2nd and 3rd parameters.

To answer your original question though, the answer is it "shouldn't". The caller of your script could be manipulating your script in a meta-programmatic way, i.e. reading the file from the disk and explicity invoking it. If it is expecting, say an 'args' variable to be present, it may fail as the 'args' variable is not populated in an advanced function (CmdletBinding). I doubt this is the case though. Based on the invocation you posted, I'd be curious to see how the CmdletBinding attribute could affect the output.

NoCmdletBinding.ps1:

Param(
[string]$name,
[string]$detaillevel,
[string]$oid
)
"Has CmdletBinding: $(Test-Path Variable:\PsCmdlet)"
"name             : $name"
"detaillevel      : $detaillevel"
"oid              : $oid"
"args.Count       : $($args.Count)"

PS> & '.\NoCmdletBinding.ps1' -name somedevicename -detaillevel 2 -oid oidstring FourthArg

Has CmdletBinding: False
name             : somedevicename
detaillevel      : 2
oid              : oidstring
args.Count       : 1

CmdletBinding.ps1:

[CmdletBinding()]
Param(
[string]$name,
[string]$detaillevel,
[string]$oid
)
"Has CmdletBinding: $(Test-Path Variable:\PsCmdlet)"
"name             : $name"
"detaillevel      : $detaillevel"
"oid              : $oid"
"args.Count       : $($args.Count)"

# Passing a 4th arg to an advanced function throws an error
PS> & '.\CmdletBinding.ps1' -name somedevicename -detaillevel 2 -oid oidstring

Has CmdletBinding: True
name             : somedevicename
detaillevel      : 2
oid              : oidstring
args.Count       : 0
skataben
  • 2,023
  • 1
  • 22
  • 25