Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set $PSDefaultParametersValues for use within Module Scope

I guess I'm just missing something, but here is my set up, i have a test module with three test functions, i want to set the $PSDefaultParameterValues to make a common parameter value that only exists to the other cmdlets in said module. Here is my code:

test.psm1:

$PSDefaultParameterValues.Clear()

"$(Split-Path -Path $MyInvocation.MyCommand.Path)\*.ps1" | Resolve-Path | % { . $_.ProviderPath }

in the same dir i have my functions:

New-TestFunction.ps1:

Function New-TestFunction
{
     [CmdletBinding()]
     [OutputType([String])]

     Param
     (
         [String]
         $InputString
     )

     $Script:PSDefaultParameterValues.Add("Write-TestValue:OutputValue", $InputString)
}

Write-TestValue.ps1:

Function Write-TestValue
{
     [CmdletBinding()]
     [OutputType()]

     Param
     (
          [Parameter()]
          [String]
          $OutputValue
     )

     Write-Output -InputObject $OutputValue
}

so, i should be able to do this:

PS C:\> New-TestFunction -InputString 'Test String'; Write-TestValue
PS C:\> 'Test String'

But it returns nothing, the $OutputValue remains $null. The only way i have gotten it to work is by using the $Global: scope, which i would rather not do. I feel like functions imported in under the Module scope should see the $Script: Scope of variables. I wrote a little test function:

Write-ScriptPSDefaultParameterValues.ps1

Function Write-ScriptPSDefaultParameterValues
{
    [CmdletBinding()]
    [OutputType()]

    Param
    (
    )

    Write-Output -InputObject $Script:PSDefaultParameterValues
}

And this works:

PS C:\>  New-TestFunction -InputString 'Test String'; Write-ScriptPSDefaultParameterValues
Name                         Value
----                         -----
Write-TestValue:OutputValue  Test String

So from within side the functions, the $Script scope exists, just not in the parameters. Maybe i just don't understand how the scopes work completely, thanks for any help and/or clarification.

like image 717
tomohulk Avatar asked Jul 07 '26 03:07

tomohulk


1 Answers

$PSDefaultParameterValues get read from scope where invocation occurs, not from scope where function defined.

New-Module {
    $PSDefaultParameterValues=@{'F:Value'=10}
    function F{
        [CmdletBinding()]
        param(
            $Value
        )
        "F:`$Value:$Value"
    }
    function G{
        [CmdletBinding()]
        param(
            $Value
        )
        "G:`$Value:$Value"
    }
    function Invoke-F{
        F
    }
    function Invoke-G{
        G
    }
}|Out-Null
$PSDefaultParameterValues=@{'G:Value'=20}
F
G
Invoke-F
Invoke-G

Results:

F:$Value:
G:$Value:20
F:$Value:10
G:$Value:

So, when function executed from global scope, then global $PSDefaultParameterValues get read. And when function executed from other module function, then module $PSDefaultParameterValues get read.

like image 173
user4003407 Avatar answered Jul 13 '26 20:07

user4003407



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!