5

The script starts off declaring a parameter:

param([Parameter(Position=0, Mandatory = $true)]$a)

When i run the script without passing any arguments, I get:

Cmdlet test.ps1 at command pipeline position 2
Supply values for the following parameters:
a: 

I would like to suppress the first line "Cmdlet test.ps1..." that sounds quite awkward to the user. Then i would like to specify a textual request for the $a parameter instead of just showing its variable-name.

2 Answers 2

7

You can't do that. The best you can do is add HelpMessage:

[Parameter(Position=0, Mandatory = $true, HelpMessage = 'Pretty message')]

Then you will see an additional line that says (Type !? for Help.)

The prompt will be the same, but if the user types !? then your message will be displayed. It's not great.

Other than that, you can make the parameter optional (remove mandatory) then test for the value, and prompt for it yourself in the body:

param([Parameter(Position=0)]$a)
if (!$a) {
    $a = Read-Host -Prompt 'Hello user, give me the value, you know the one'
}
Sign up to request clarification or add additional context in comments.

2 Comments

Another possibility would be to leave the parameter optional, but specify it as param([Parameter(Position=0)]$a=Read-Host -Prompt 'Gimme!'; this will use whatever parameter is supplied on the command line if one is, but if not, will prompt with the indicated prompt. Read-Host, however, may not be the best way to do it since it doesn't have a way of specifying that the input can't be equivalent to $null.
@JeffZeitlin yeah you could do it as a default value too. If you add [ValidateNotNullOrEmpty()] (or any other validation attribute to the parameter) it should apply with either your or my method (it sticks with the variable).
1

Try it like that:

param (
    [Parameter(Mandatory)]
    [Alias("SourceKeyVault")]
    [string]${Source key vault},
    
    [Parameter(Mandatory)]
    [Alias("DestinationKeyVault")]
    [string]${Destination key vault}
)

$SourceKeyVault = ${Source key vault}
$DestinationKeyVault = ${Destination key vault}

Mind that you cannot use special symbols in parameter descriptions in this way. Afterwards, inside your script, just use the $SourceKeyVault etc (example) values.

Comments

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.