Skip to content

$PSScriptRoot is not populated when used in a default value for a non-mandatory script parameter #4688

Closed

Description

Steps to reproduce

Run this script (via PowerShell -File ....)

param(
    [Parameter(Mandatory = $false)]
    [string] $Foo = 'hi\{0}\hi' -f $PSScriptRoot
)

Write-Host $Foo

Function Bar {
    param(
        [Parameter(Mandatory = $false)]
        [string] $Bar = $PSScriptRoot
    )
    Write-Host $Bar
}

Function Baz {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false)]
        [string] $Baz = $PSScriptRoot
    )
    Write-Host $Baz
}

Bar
Baz

Expected behavior

hi\C:\tmp\hi
C:\tmp
C:\tmp

Actual behavior

hi\\hi
C:\tmp
C:\tmp

Environment data

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

[edited by Dongbo Wang (@daxian-dbw) to put the repro in code blocks]

Activity

  1. mklement0 commented on Aug 28, 2017

    @mklement0
    Contributor

    Actually, what matters is whether the script is an advanced (cmdlet-like) script or not.
    By using a [Parameter(...)] attribute, you're implicitly making your script an advanced script, even without decorating your param(...) block with an explicit [CmdletBinding()] attribute.

    Combining an advanced script with invocation via powershell -File is what produces the symptom, as the following simplified script demonstrates:

    Save the following as t.ps1 in the current dir:

    [CmdletBinding()]
    param(
      $Foo = $PSScriptRoot
    )
    
    "[$Foo]"
    # Direct invocation works fine.
    > ./t.ps1
    [/Users/jdoe]
    
    # Invocation via the external CLI with -File shows the symptom:
    > powershell -File ./t.ps1
    []

    Were you to omit [CmdletBinding()], the symptom would not surface.


    Two asides:

    • Your version table shows a Windows PowerShell version. In this case, the symptom also surfaces in PowerShell Core. Generally, you should only report issues here that (also) surface in PS Core, using the latest released version. If a symptom surfaces only in Windows PS, report it on uservoice.com.

    • Please use the fenced code and output blocks provided in the issue template (```powershell ... and ```none ...) - it makes for much more readable representations.

  2. added
    Issue-Question
    WG-Language
    on Aug 28, 2017
  3. daxian-dbw commented on Aug 30, 2017

    @daxian-dbw
    Member

    Thanks Michael Klement (@mklement0) for the analysis and simple repro! It's really helpful.
    It also repros when you dot source a script cmdlet in powershell, for example:

    # use the same 't.ps1'
    > . .\t.ps1
    []

    This is because the automatic variables are not set up before parameter binding for a script cmdlet when they are not going to run in new local scopes. I'm preparing a fix.

  4. LaurentDardenne commented on Feb 16, 2018

    @LaurentDardenne

    Steve Lee (@SteveL-MSFT)
    This fix does not have the 'Breaking-Change' label ? Is it correct ?

  5. added
    Breaking-Change
    on Feb 21, 2018
  6. SteveL-MSFT commented on Feb 21, 2018

    @SteveL-MSFT
    Member

    Laurent Dardenne (@LaurentDardenne) agree this is technically a breaking change

  7. iSazonov commented on Feb 21, 2018

    @iSazonov
    Collaborator

    :-) Any bug fix is a breaking change. I guess this label is useless here.

  8. SteveL-MSFT commented on Feb 21, 2018

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) valid point, I've been thinking about having different degrees of Breaking Change labels as they are not all equal

  9. iSazonov commented on Feb 22, 2018

    @iSazonov
    Collaborator

    It make sense only if we want strongly document breaking change buckets in docs and changelog.

  10. removed
    Breaking-Change
    on Feb 22, 2018
  11. SteveL-MSFT commented on Feb 22, 2018

    @SteveL-MSFT
    Member

    I believe we've been trying to use Breaking-Change label on issues/PRs that we believe will have a high chance of impact or that the impact is high if encountered. Agree that technically any bug fix is a breaking change. So as to not dilute this label, I'm removing it from this issue as it's unlikely anyone is expecting the previous behavior.

  12. FireEmerald commented on Nov 26, 2021

    @FireEmerald

    Steve Lee (@SteveL-MSFT) Please reopen this issue, it still occurs in powershell v7.2.0. Or am i missing something? In which version was the bug fixed?

    PS C:\scripts> $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      7.2.0
    PSEdition                      Core
    GitCommitId                    7.2.0
    OS                             Microsoft Windows 10.0.19042
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
    PS C:\scripts> powershell -File C:\scripts\test.ps1
    hello \world
    # Script test.ps1
    [CmdletBinding()]
    param(
      [Parameter(Mandatory=$false)][string] $aParam = "$PSScriptRoot\world"
    )
    Write-Host "hello $aParam"
  13. iSazonov commented on Nov 26, 2021

    @iSazonov
    Collaborator
    PS C:\scripts> powershell -File C:\scripts\test.ps1

    FireEmerald I wonder you run Windows PowerShell instead of pwsh

  14. rkeithhill commented on Nov 26, 2021

    @rkeithhill
    Collaborator

    RE Ilya (@iSazonov)'s powershell (Windows PowerShell) vs pwsh (PowerShell v6/7) question:
    image

  15. FireEmerald commented on Nov 29, 2021

    @FireEmerald

    Ilya (@iSazonov) Because pwsh does not exist on the target machine? Seems like the non core version of powershell doesn't contain the fix for this issue. I should also have posted the version of the runtime called via powershell:

    powershell -Command "(Get-Variable PSVersionTable -ValueOnly).PSVersion"
    
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      1      19041  1320
  16. iSazonov commented on Nov 29, 2021

    @iSazonov
    Collaborator

    Seems like the non core version of powershell doesn't contain the fix for this issue.

    Yes, Windows PowerShell gets only security fixes, all new features are added only to pwsh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-QuestionResolution-FixedWG-Language

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions