Skip to content

Dot sourcing a function does not allow $MyInvocation to be used as a default parameter #4840

@RamblingCookieMonster

Description

@RamblingCookieMonster

It can be useful to run functions (vs. scripts) in the current scope. @Jaykul's Get-ParameterValues is an example

At the moment, it appears that a dot sourced function does not actually run in the current scope. Apologies if I'm using the wrong phrasing, the example should clarify things:

Steps to reproduce

function Test-MyInvocationParam {
    [cmdletbinding()]
    param($Invocation = $MyInvocation)
    $Invocation
}

function Test-Bug {
    [cmdletbinding()]
    param($a=1)
    . Test-MyInvocationParam
}

(Test-Bug).MyCommand.Name

Expected behavior

Test-Bug

Actual behavior

Test-MyInvocationParam

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.7
OS                             Darwin 16.7.0 Darwin Kernel Version 16.7.0: Th...
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-By DesignThe reported behavior is by design.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions