Powershell Dynamic conditional parametersSet a default parameter value for a JavaScript functionDoes Java support default parameter values?How can I pass a parameter to a setTimeout() callback?Determine installed PowerShell versionHow to run a PowerShell scriptPowerShell says “execution of scripts is disabled on this system.”How to mark a switch parameter as mandatory in PowershellHow do you comment out code in PowerShell?How are parameters sent in an HTTP POST request?What are the -Xms and -Xmx parameters when starting JVM?
What is the maximum amount of diamond in one Minecraft game?
What is this airplane with small wings at different angles seen at Paphos Airport?
What's the big deal about the Nazgûl losing their horses?
Park the computer
Attach a visible light telescope to the outside of the ISS
Removing polygon holes in OpenLayers
Minor differences between two recorded guitars
PhD: When to quit and move on?
Is reasonable to assume that the 食 in 月食/日食 can be interpreted as the sun/moon being "eaten" during an eclipse?
How do I talk to my wife about unrealistic expectations?
Machine Learning Golf: Multiplication
Can you take the Dodge action while prone?
What is the highest level of accuracy in motion control a Victorian society could achieve?
Any way to meet code with 40.7% or 40.44% conduit fill?
Why no parachutes in the Orion AA2 abort test?
Do I need transit visa for Dublin?
Shipped package arrived - didn't order, possible scam?
Why does "sattsehen" take accusative "mich", not dative "mir"? Even though it is not "me" that I'm looking at?
How predictable is $RANDOM really?
Bringing coumarin-containing liquor into the USA
Initializing variables in an "if" statement
Is there a standard definition of the "stall" phenomena?
Will Jimmy fall off his platform?
Can a USB hub be used to access a drive from two devices?
Powershell Dynamic conditional parameters
Set a default parameter value for a JavaScript functionDoes Java support default parameter values?How can I pass a parameter to a setTimeout() callback?Determine installed PowerShell versionHow to run a PowerShell scriptPowerShell says “execution of scripts is disabled on this system.”How to mark a switch parameter as mandatory in PowershellHow do you comment out code in PowerShell?How are parameters sent in an HTTP POST request?What are the -Xms and -Xmx parameters when starting JVM?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom: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
)
powershell parameters conditional
add a comment |
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
)
powershell parameters conditional
add a comment |
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
)
powershell parameters conditional
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
)
powershell parameters conditional
powershell parameters conditional
asked Mar 25 at 16:58
Cleadus FetusCleadus Fetus
485 bronze badges
485 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
This is excellent. Thank you.
– Cleadus Fetus
Mar 27 at 17:23
no worries, glad it can help you out.
– postanote
Mar 27 at 18:10
add a comment |
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342918%2fpowershell-dynamic-conditional-parameters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
This is excellent. Thank you.
– Cleadus Fetus
Mar 27 at 17:23
no worries, glad it can help you out.
– postanote
Mar 27 at 18:10
add a comment |
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
This is excellent. Thank you.
– Cleadus Fetus
Mar 27 at 17:23
no worries, glad it can help you out.
– postanote
Mar 27 at 18:10
add a comment |
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
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
answered Mar 26 at 2:36
postanotepostanote
4,9102 gold badges4 silver badges11 bronze badges
4,9102 gold badges4 silver badges11 bronze badges
This is excellent. Thank you.
– Cleadus Fetus
Mar 27 at 17:23
no worries, glad it can help you out.
– postanote
Mar 27 at 18:10
add a comment |
This is excellent. Thank you.
– Cleadus Fetus
Mar 27 at 17:23
no worries, glad it can help you out.
– postanote
Mar 27 at 18:10
This is excellent. Thank you.
– Cleadus Fetus
Mar 27 at 17:23
This is excellent. Thank you.
– Cleadus Fetus
Mar 27 at 17:23
no worries, glad it can help you out.
– postanote
Mar 27 at 18:10
no worries, glad it can help you out.
– postanote
Mar 27 at 18:10
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 25 at 20:27
HAL9256HAL9256
5,41315 silver badges32 bronze badges
5,41315 silver badges32 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342918%2fpowershell-dynamic-conditional-parameters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown