2

Say I have a Powershell script TestParameters.ps1 like this, with two mandatory, named parameters and two optional parameters:

[CmdletBinding()]
Param (
[Parameter(Mandatory=$True)]
[string] $AFile = "C:\A\Path",

[Parameter(Mandatory=$True)]
[ValidateSet("A","B","C", "D")]
[string] $ALetter = "A",

[Parameter(Mandatory=$False)]
[ValidateNotNullOrEmpty()]
[string] $Optional1 = "Foo",

[Parameter(Mandatory=$False)]
[ValidateNotNullOrEmpty()]
[string] $Optional2 = "Bar"
)

echo "Hello World!"
$psboundparameters.keys | ForEach {
    Write-Output "($_)=($($PSBoundParameters.$_))"
}

Say I call the script like this:

.\TestParameters.ps1 `
    -AFile                 "C:\Another\Path" `
    -ALetter               "B"

which produces output:

Hello World!
(AFile)=(C:\Another\Path)
(ALetter)=(B)

Powershell set the variables $Optional1 and $Optional2 ... but how do I easily display them to the screen, like the way I use $PSBoundParameters?

I do not want to simply write the following every time I have a script:

Write-Host $AFile
Write-Host $ALetter
Write-Host $Optional1
Write-Host $Optional2

Notes:

  • $args only seems to include unbound parameters, not default parameters
  • $MyInvocation seems to only include commands passed on the command lineEDIT: MyInvocation has member variable MyCommand.Parameters, which seems to have all the parameters, not just those passed on the command line...see the accepted answer, below.
  • Get-Variable seems to include the optional variables in the result list, but I do not know how to differentiate them from the other variables

2 Answers 2

4

The following seems to work on my box... probably not the best way to do it, but it seems to work in this case, at least...

[cmdletbinding()]

param([Parameter(Mandatory=$True)]
[string] $AFile = "C:\A\Path",

[Parameter(Mandatory=$True)]
[ValidateSet("A","B","C", "D")]
[string] $ALetter = "A",

[Parameter(Mandatory=$False)]
[ValidateNotNullOrEmpty()]
[string] $Optional1 = "Foo",

[Parameter(Mandatory=$False)]
[ValidateNotNullOrEmpty()]
[string] $Optional2 = "Bar"
)

echo "Hello World!"

($MyInvocation.MyCommand.Parameters ).Keys | %{
    $val = (Get-Variable -Name $_ -EA SilentlyContinue).Value
    if( $val.length -gt 0 ) {
        "($($_)) = ($($val))"
    }
}

Saved as allparams.ps1, and run it looks like:

.\allparams.ps1 -ALetter A -AFile "C:\Another\Path" 
Hello World!
(AFile) = (C:\Another\Path)
(ALetter) = (A)
(Optional1) = (Foo)
(Optional2) = (Bar)
Sign up to request clarification or add additional context in comments.

Comments

1

Using AST:

[CmdletBinding()]
Param (
 [Parameter(Mandatory=$True)]
 [string] $AFile = "C:\A\Path",

 [Parameter(Mandatory=$True)]
 [ValidateSet("A","B","C", "D")]
 [string] $ALetter = "A",

 [Parameter(Mandatory=$False)]
 [ValidateNotNullOrEmpty()]
 [string] $Optional1 = "Foo",

 [Parameter(Mandatory=$False)]
 [ValidateNotNullOrEmpty()]
 [string] $Optional2 = "Bar"
)

echo "Hello World!"
$psboundparameters.keys | ForEach {
    Write-Output "($_)=($($PSBoundParameters.$_))"
}


$ast = [System.Management.Automation.Language.Parser]::
   ParseFile($MyInvocation.InvocationName,[ref]$null,[ref]$Null) 

$ast.ParamBlock.Parameters | select Name,DefaultValue

4 Comments

Thank you, mjolinor : o ) I have confirmed this also works--nicely done. I made a determination that the accepted answer seems a bit cleaner, though, for general-purpose use. Thanks again.
The accepted answer appears to tell you what the values are that were passed (the same as $PsBoundParameters), not what the defaults are.
Thank you for clarifying--this represents very useful info! I missed this distinction, initially. For my use case, I want to log the script parameters to a file, so we can review the script behavior later...so I want the values actually passed, if present, rather than the default values. This is useful knowledge, though, which may come in helpful in the future; thank you.
If that's the case then the accepted answer is correct for your application. That AST soulution would be for situations when you want to display the defaults, regardless of what the passed values were.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.