Comments (2)
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.
from powershell.
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
PowerShell 5.1:
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
PowerShell 7.3.9
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
PowerShell 7.4.0-rc1
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
PowerShell 7.4.1
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
PowerShell 7.4.2
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
PowerShell 7.5.0-preview.2
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
from powershell.
Related Issues (20)
- ConvertTo-Json for empty array produces null HOT 9
- Expose default PowerShell User-Agent in Microsoft.PowerShell.Commands.PSUserAgent
- LICENSE.txt
- HideTableHeaders is not persisted to Get-FormatData if defined as FormatData rather than in format file. HOT 1
- Using Invoke-WebRequest POST to upload a file is broken HOT 19
- ScriptProperty getters (-Value) can not throw errors HOT 8
- call operator & fails when used with background operator & HOT 6
- "RegisterMicrosoftUpdate.ps1" fails and freezes installer when AppLocker / WDAC rules exist (despite the script being whitelisted & running full language mode) HOT 2
- Cannot build packages from devcontainer
- Invoke-WebRequest fails with โRequest headers must contain only ASCII characters.โ for simple requests HOT 2
- [JEA] Register-PSSessionConfiguration does not "cascade" the -NoServiceRestart parameter to Set-PSSessionConfiguration when automatically calling it.
- Feature: Expose `Set-Location` history stack HOT 11
- Surrogate pairs cause misalignment in Format-Wide HOT 5
- Update behavior for getting wix 3.14 in CI.psm1 HOT 1
- is it defined/documented which newlines are used by Write-Host and Out-File HOT 8
- `Remove-Item` throws "Attempted to divide by zero" on PS 7.5.0-preview.3 HOT 6
- `using namespace` overrides previous `using namespace` uses as interactive HOT 3
- [Windows] No error when using a non-executable file in script line HOT 4
- `Remove-Item` reports incorrect number of removed items in the progress bar HOT 1
- Bug HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell.