Skip to content

PowerShell class methods cannot invoke non-exported functions. #4713

@alx9r

Description

@alx9r

Steps to reproduce

New-Module m {
    class c { 
        [object] Invoke () { return Invoke-Something }
    }
    function Invoke-Something { 'Invoke-Something' }
    function New-InstanceOfC { [c]::new() }
    function Invoke-MethodOfC { (New-InstanceOfC).Invoke() }
    Export-ModuleMember *OfC
} | Out-Null

(New-InstanceOfC).Invoke()
Invoke-MethodOfC

Expected behavior

I expected both calls to Invoke-Something to succeed and output

Invoke-Something
Invoke-Something

Actual behavior

Neither call to Invoke-Something succeeds.

Invoke-Something : The term 'Invoke-Something' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\un1\Desktop\test.ps1:3 char:37
+         [object] Invoke () { return Invoke-Something }
+                                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Invoke-Something:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Invoke-Something : The term 'Invoke-Something' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\un1\Desktop\test.ps1:3 char:37
+         [object] Invoke () { return Invoke-Something }
+                                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Invoke-Something:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.6
OS                             Microsoft Windows 6.3.9600
Platform                       Win32NT
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

    KeepOpenThe bot will ignore these and not auto-close

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions