Reputation: 326
Is it possible to have PowerShell display the help messages by default when parameters are not specified in the command line without the user having to input "!?" for help?
Should I not use param and do it manualy instead with Read-Host if I want my script to be interactive?
param (
[Parameter(Mandatory=$true,HelpMessage="Enter desired password.")][string]$desired_password,
[Parameter(Mandatory=$true,HelpMessage="Please input target hostnames.")][string[]]$target_hosts
)
What would be the best approach in such case?
Upvotes: 4
Views: 5222
Reputation: 1761
Unfortunately, currently it's not possible to make a friendly prompt for a missing parameter without Read-Host. But this could be done in a more elegant manner than in Bill's answer:
param(
[String] $TestParameter=$(Read-Host -prompt "Enter the test parameter")
)
Upvotes: 2
Reputation: 24585
If you want some help text to always be displayed if you do not specify a [String]
parameter, then yes, you have to write this yourself. Example:
param(
[String] $TestParameter
)
if ( -not $TestParameter ) {
Write-Host "This is help for -TestParameter."
while ( -not $TestParameter ) {
$TestParameter = Read-Host "Enter a value"
}
}
"Argument for -TestParameter: $TestParameter"
Upvotes: 1