Do the parameter attributes change the powershell script file somehow?

I use the net-snmp extension functions to be able to run the powershell script when requesting a specific SNMP file.

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

The script is called by the net-snmp agent as follows:

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

However, when I add parameter attributes or CmdletBinding (or both), my parameter definitions in my get-storageinfo.ps1 script change. I do not 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 same problem

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

This somehow breaks my snmpd functions. When the configured OID is read, I get: "There is no such instance in this OID"

( ) , CmdletBinding :

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

net-snmp , . Net-SNMP (snmpd) script , script /cmdletbinding. - , (snmpd). .

, net-snmp? .

UPDATE "extend" snmpd.conf istead "pass" commando. . , . , . , .

+4
1

, , , 2- 3- .

, "". script script - , .. . , , args, , "args" (CmdletBinding). , . , , CmdletBinding .

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
0

Source: https://habr.com/ru/post/1530119/


All Articles