0

I am writing a script and I want to specify the parameters to do the following:

Parameter 1 is action (either check or kill) Parameter 2 is computername.

If neither parameter is specified I want my Usage information displayed Parameter 2 should ONLY be prompted if Parameter 1 is specified.

Param(
    [Parameter(Mandatory=$True,
    HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
    [String]$Action,

    [Parameter(Mandatory = $false,
    Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
    [ValidateNotNullorEmpty()]
    [String]$Computers 
    )

2 Answers 2

1

Why force users to guess at what input is expected? Just tell them up front what is expected.

For example:

Function Test-DescriptiveUserPrompt
{
    [CmdletBinding()]

    Param
    (
        [ValidateSet('C','K')]
        [string]$Action = $(
        Write-Host '
        Please Enter an Action. (C)heck, (K)ill:   ' -ForegroundColor Yellow -NoNewLine
        Read-Host 
            ),

        [ValidateNotNullorEmpty()]
        [string[]]$Computers = $(
        Write-Host '
        Please Enter One or More Hostnames. separate multiple hostnames with a comma. 
        EXAMPLE: Hostname1,Hostname2:   ' -ForegroundColor Yellow -NoNewLine
        Read-Host 
        )
    )

    Process
    {
            "You choose $Action"
            "You enter the list $Computers"
    }
}


# Results

Test-DescriptiveUserPrompt

        Please Enter an Action. (C)heck, (K)ill:   c

        Please Enter One or More Hostnames. seperate multiple hostnames with a comma. 
        EXAMPLE: Hostname1,Hostname2:   localhost,remotehost
c
localhost,remotehost


Test-DescriptiveUserPrompt -Action C -Computers localhost,remotehost

C
localhost
remotehost
Sign up to request clarification or add additional context in comments.

Comments

0

The quick and dirty and simpler way of doing things is to use Parameter Sets. In this case, it will default to displaying the Usage information if things aren't correct.

Function Test {
    [CmdletBinding()]
Param(
    [Parameter(Mandatory=$true, ParameterSetName = "Action",
    HelpMessage="Please Enter an Action. (C)heck, (K)ill, or (?) for usage")]
    [ValidateSet("C","K","?")]
    [Parameter(Mandatory=$false, ParameterSetName = "Usage")]
    [String]$Action,

    [Parameter(Mandatory = $true, ParameterSetName = "Action",
    Helpmessage="Please Enter One or More Hostnames. seperate multiple hostnames with an , EXAMPLE: Hostname1,Hostname2")]
    [ValidateNotNullorEmpty()]
    [String]$Computers 
    )
Process
{
    if($PSCmdlet.ParameterSetName -eq "Usage" -or $Action -eq "?")
    {
        Write-Host "Usage"
    }
    else
    {
        Write-Host "Action"
        Write-Host $Action
        Write-Host $Computers
    }
}
}

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.