I'm trying to write some code that requires examining the functions metadata and processes some other data based on the command's parameters. In the process, I've discovered some really strange behavior that I can't figure out.
I have a function in a psm1 script module, and it's loaded by importing a neighboring psd1 module manifest that declares it as a nested module. I've declared 14 parameters on it explicitly. When I Get-Command
and examine the Parameters
, I can see it has 23 parameters. 14 are mine; the rest are common parameters.
PS> (Get-Command Install-MyFunctionFromModule).Parameters.Count
23
PS> (Get-Command Install-MyFunctionFromModule).Parameters.keys
MyParameter1
MyParameter2
MyParameter3
MyParameter4
MyParameter5
MyParameter6
MyParameter7
MyParameter8
MyParameter9
MyParameter10
MyParameter11
MyParameter12
MyParameter13
MyParameter14
Verbose
Debug
ErrorAction
WarningAction
ErrorVariable
WarningVariable
OutVariable
OutBuffer
PipelineVariable
(My functions's name does start with Install-
. My parameters don't end with numbers, though. These are just dummy placeholders because I didn't want to use the real names.)
This is kind of expected. Functions are supposed to have common parameters.
But in order to test my code that deals with the parameters, I tried creating a test module. Here's what it looks like:
testfun.psm1
:
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
function test-noparams() {
Write-Host 'None'
}
function test-differentnamedparams([string]$hello, [switch]$bye) {
Write-Host "names that don't conflict with common params"
}
Then when I import the module, neither functions have any common parameters:
PS> Import-Module .\testfunc.psm1
PS> (Get-Command test-differentnamedparams).Parameters.Count
2
PS> (Get-Command test-differentnamedparams).Parameters | Format-Table -AutoSize
Key Value
--- -----
hello System.Management.Automation.ParameterMetadata
bye System.Management.Automation.ParameterMetadata
PS> (Get-Command test-noparams).Parameters.Count
0
I've tried a few things to see if they made a difference:
- Exporting the functions explicitly using
Export-ModuleMember
- Use a manifest that imports the script module as a
NestedModule
- Using a script and including it via
. .\testfunc.ps1
instead of a module - Defining the function directly in the shell
None of them changed anything.
What determines whether or not a function has common parameters or not? What could cause them not to?
Aucun commentaire:
Enregistrer un commentaire