Skip to content

PowerShell class defined in 'New-Module -ScriptBlock' doesn't work as expected #2841

@daxian-dbw

Description

@daxian-dbw

Steps to reproduce

New-Module -Name ReloadingTest -ScriptBlock {
 $passedArgs = $args
 class Root { $passedIn = $passedArgs }
 function Get-PassedArgsRoot { [Root]::new().passedIn }
 function Get-PassedArgsNoRoot { $passedArgs }
} -ArgumentList 'abc'

Get-PassedArgsRoot

Expected behavior

Return 'abc'

Actual behavior

Return nothing

PSv5.1

PS:3> New-Module -Name ReloadingTest -ScriptBlock {
>>  $passedArgs = $args
>>  class Root { $passedIn = $passedArgs }
>>  function Get-PassedArgsRoot { [Root]::new().passedIn }
>>  function Get-PassedArgsNoRoot { $passedArgs }
>> } -ArgumentList 'abc'

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        ReloadingTest                       {Get-PassedArgsNoRoot, Get-PassedArgsRoot}


[C:\]
PS:4> Get-PassedArgsRoot
[C:\]
PS:5> Get-PassedArgsNoRoot
abc
[C:\]
PS:6> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.206
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.206
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PSv6.0.0-alpha.13

PS C:\> New-Module -Name ReloadingTest -ScriptBlock {
>>  $passedArgs = $args
>>  class Root { $passedIn = $passedArgs }
>>  function Get-PassedArgsRoot { [Root]::new().passedIn }
>>  function Get-PassedArgsNoRoot { $passedArgs }
>> } -ArgumentList 'abc'

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        ReloadingTest                       {Get-PassedArgsNoRoot, Get-PassedArgsRoot}


PS C:\>
PS C:\> Get-PassedArgsRoot
PS C:\> Get-PassedArgsNoRoot
abc
PS C:\> $PSVersionTable

Name                           Value
----                           -----
BuildVersion                   3.0.0.0
WSManStackVersion              3.0
PSEdition                      Core
PSRemotingProtocolVersion      2.3
CLRVersion
SerializationVersion           1.1.0.1
GitCommitId                    v6.0.0-alpha.13
PSVersion                      6.0.0-alpha
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

Metadata

Metadata

Assignees

Labels

Resolution-No ActivityIssue has had no activity for 6 months or moreWG-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