Open BasBerk opened 2 months ago
It's because of your return statement. You can't assign the value of a return statement to a variable, even if you wrap it in a subexpression. See the following demonstration:
$Res = return "Hello"
$Res | Out-Host
$Res = $(return "Hello")
$Res | Out-Host
$Res = "Hello"
$Res | Out-Host
This behavior is consistent with what I see in 5.1 so I doubt 7.3 behaved differently. Most likely you were just hitting the branch without the return statement when you were using 7.3.
@BasBerk @MartinGC94
I've also seen this behavioural change. We noticed this from one of our Pester tests failing in our CI. I believe this issue is related to the sub-expression operator.
Seems to be that PowerShell versions 7.4.0-rc.1
, 7.4.1
, 7.5.0-preview.2
are affected. Interestingly, PowerShell 7.4.2
is not affected.
Take the simple example below, using the sub-expression operator:
function Get-Planet {
[CmdletBinding()]
param (
$Planet = $(
return "Earth"
)
)
process {
Write-Output -InputObject "Planet is $Planet"
}
}
Get-Planet
PS > $PSVersionTable
Name Value
---- -----
PSVersion 5.1.22000.2713
...
PS > function Get-Planet {
>> [CmdletBinding()]
>> param (
>> $Planet = $(
>> return "Earth"
>> )
>> )
>>
>> process {
>> Write-Output -InputObject "Planet is $Planet"
>> }
>> }
PS > Get-Planet
Planet is Earth
docker run -it mcr.microsoft.com/powershell:7.3-ubuntu-22.04
PowerShell 7.3.9
PS /> function Get-Planet {
>> [CmdletBinding()]
>> param (
>> $Planet = $(
>> return "Earth"
>> )
>> )
>>
>> process {
>> Write-Output -InputObject "Planet is $Planet"
>> }
>> }
PS /> Get-Planet
Planet is Earth
docker run -it mcr.microsoft.com/powershell:preview-7.4-ubuntu-22.04
PowerShell 7.4.0-rc.1
PS /> function Get-Planet {
>> [CmdletBinding()]
>> param (
>> $Planet = $(
>> return "Earth"
>> )
>> )
>>
>> process {
>> Write-Output -InputObject "Planet is $Planet"
>> }
>> }
PS /> Get-Planet
Planet is
PS > $PSVersionTable
Name Value
---- -----
PSVersion 7.4.1
...
PS > function Get-Planet {
>> [CmdletBinding()]
>> param (
>> $Planet = $(
>> return "Earth"
>> )
>> )
>>
>> process {
>> Write-Output -InputObject "Planet is $Planet"
>> }
>> }
PS > Get-Planet
Planet is
docker run -it mcr.microsoft.com/powershell:7.4-ubuntu-22.04
PowerShell 7.4.2
PS /> function Get-Planet {
>> [CmdletBinding()]
>> param (
>> $Planet = $(
>> return "Earth"
>> )
>> )
>>
>> process {
>> Write-Output -InputObject "Planet is $Planet"
>> }
>> }
PS /> Get-Planet
Planet is Earth
docker run -it mcr.microsoft.com/powershell:preview-7.5-ubuntu-22.04
PowerShell 7.5.0-preview.2
PS /> function Get-Planet {
>> [CmdletBinding()]
>> param (
>> $Planet = $(
>> return "Earth"
>> )
>> )
>>
>> process {
>> Write-Output -InputObject "Planet is $Planet"
>> }
>> }
PS /> Get-Planet
Planet is
Prerequisites
Steps to reproduce
In powershell 7.3 we could have expressions as default for the parameters:
simplified:
Starting from 7.4.1 this is not working anymore.
Expected behavior
Actual behavior
Error details
No response
Environment data
Visuals
No response