powershell parameters command line menu
I wish to create a script which accepts input from the command line and based on the first value, it then determines the next parameters on offer.
Such as to determine if you are to do a single Run or a Batch run for a Password change operation:
./script.ps1 -singleMODE -UserName -Password
./script.ps1 -batchMODE -filename
What i am confused about whilst learning Powershell is what this is? I have looked at Parameters and can read them into variables from command line...but what i want as above has some logic and i am a bit lost. Can someone give me a nudge as to what this is called....and then i can continue my googling! :)
I am thinking somehow i combine Params and Functions so it flicks to different blocks...but i am guessing
any help appreciated! :)
cheers
powershell powershell-v3.0
add a comment |
I wish to create a script which accepts input from the command line and based on the first value, it then determines the next parameters on offer.
Such as to determine if you are to do a single Run or a Batch run for a Password change operation:
./script.ps1 -singleMODE -UserName -Password
./script.ps1 -batchMODE -filename
What i am confused about whilst learning Powershell is what this is? I have looked at Parameters and can read them into variables from command line...but what i want as above has some logic and i am a bit lost. Can someone give me a nudge as to what this is called....and then i can continue my googling! :)
I am thinking somehow i combine Params and Functions so it flicks to different blocks...but i am guessing
any help appreciated! :)
cheers
powershell powershell-v3.0
You are probably looking for parameter sets.
– Bill_Stewart
Nov 19 '18 at 19:33
add a comment |
I wish to create a script which accepts input from the command line and based on the first value, it then determines the next parameters on offer.
Such as to determine if you are to do a single Run or a Batch run for a Password change operation:
./script.ps1 -singleMODE -UserName -Password
./script.ps1 -batchMODE -filename
What i am confused about whilst learning Powershell is what this is? I have looked at Parameters and can read them into variables from command line...but what i want as above has some logic and i am a bit lost. Can someone give me a nudge as to what this is called....and then i can continue my googling! :)
I am thinking somehow i combine Params and Functions so it flicks to different blocks...but i am guessing
any help appreciated! :)
cheers
powershell powershell-v3.0
I wish to create a script which accepts input from the command line and based on the first value, it then determines the next parameters on offer.
Such as to determine if you are to do a single Run or a Batch run for a Password change operation:
./script.ps1 -singleMODE -UserName -Password
./script.ps1 -batchMODE -filename
What i am confused about whilst learning Powershell is what this is? I have looked at Parameters and can read them into variables from command line...but what i want as above has some logic and i am a bit lost. Can someone give me a nudge as to what this is called....and then i can continue my googling! :)
I am thinking somehow i combine Params and Functions so it flicks to different blocks...but i am guessing
any help appreciated! :)
cheers
powershell powershell-v3.0
powershell powershell-v3.0
asked Nov 19 '18 at 19:27
blop001blop001
52
52
You are probably looking for parameter sets.
– Bill_Stewart
Nov 19 '18 at 19:33
add a comment |
You are probably looking for parameter sets.
– Bill_Stewart
Nov 19 '18 at 19:33
You are probably looking for parameter sets.
– Bill_Stewart
Nov 19 '18 at 19:33
You are probably looking for parameter sets.
– Bill_Stewart
Nov 19 '18 at 19:33
add a comment |
1 Answer
1
active
oldest
votes
What you need are Parameter Sets
This is a demo with a function but it works just as well with a script (just put the param block at the top.)
function Demo {
param(
[Parameter(ParameterSetName='Funk')][switch]$Funk,
[Parameter(ParameterSetName='Rock')][switch]$Rock,
[Parameter(ParameterSetName='Funk')][string]$WriteFunk,
[Parameter(ParameterSetName='Rock')][string]$WriteRock
)
if($Funk){
foreach ($C in $WriteFunk.ToCharArray()){
$N = 0..15 | Get-Random
Write-Host $C -ForegroundColor $N -BackgroundColor $(15-$N) -NoNewline
}
Write-Host ''
}
if($Rock){
Write-Host $WriteRock -ForegroundColor Gray -BackgroundColor DarkGray
}
}
Demo -Funk -WriteFunk "Melt your brain"
Demo -Rock -WriteRock "Riders on the storm"
Thanks - that is awesome! And the code example gave me a smile!!! :)....Actually if i try and use that code snippet outside of the Function...i get an Ambigous Parameter Set error. Within the Function it is perfect
– blop001
Nov 19 '18 at 20:52
that would be because I switched from metal to rock and disco to funk halfway through writing it and forgot to switch all the variables, it's fixed now
– BaronW
Nov 19 '18 at 20:59
Thanks!!!! I have been able to adapt it successfully....well so far anyway. cheers
– blop001
Nov 19 '18 at 21:34
no problem, also note that you don't need to use the first argument as a dedicated switch, it is smart enough to know which set you are in by the variables that are used. I recommend investigating $PSCmdlet.ParameterSetName
– BaronW
Nov 19 '18 at 21:48
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%2f53381362%2fpowershell-parameters-command-line-menu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you need are Parameter Sets
This is a demo with a function but it works just as well with a script (just put the param block at the top.)
function Demo {
param(
[Parameter(ParameterSetName='Funk')][switch]$Funk,
[Parameter(ParameterSetName='Rock')][switch]$Rock,
[Parameter(ParameterSetName='Funk')][string]$WriteFunk,
[Parameter(ParameterSetName='Rock')][string]$WriteRock
)
if($Funk){
foreach ($C in $WriteFunk.ToCharArray()){
$N = 0..15 | Get-Random
Write-Host $C -ForegroundColor $N -BackgroundColor $(15-$N) -NoNewline
}
Write-Host ''
}
if($Rock){
Write-Host $WriteRock -ForegroundColor Gray -BackgroundColor DarkGray
}
}
Demo -Funk -WriteFunk "Melt your brain"
Demo -Rock -WriteRock "Riders on the storm"
Thanks - that is awesome! And the code example gave me a smile!!! :)....Actually if i try and use that code snippet outside of the Function...i get an Ambigous Parameter Set error. Within the Function it is perfect
– blop001
Nov 19 '18 at 20:52
that would be because I switched from metal to rock and disco to funk halfway through writing it and forgot to switch all the variables, it's fixed now
– BaronW
Nov 19 '18 at 20:59
Thanks!!!! I have been able to adapt it successfully....well so far anyway. cheers
– blop001
Nov 19 '18 at 21:34
no problem, also note that you don't need to use the first argument as a dedicated switch, it is smart enough to know which set you are in by the variables that are used. I recommend investigating $PSCmdlet.ParameterSetName
– BaronW
Nov 19 '18 at 21:48
add a comment |
What you need are Parameter Sets
This is a demo with a function but it works just as well with a script (just put the param block at the top.)
function Demo {
param(
[Parameter(ParameterSetName='Funk')][switch]$Funk,
[Parameter(ParameterSetName='Rock')][switch]$Rock,
[Parameter(ParameterSetName='Funk')][string]$WriteFunk,
[Parameter(ParameterSetName='Rock')][string]$WriteRock
)
if($Funk){
foreach ($C in $WriteFunk.ToCharArray()){
$N = 0..15 | Get-Random
Write-Host $C -ForegroundColor $N -BackgroundColor $(15-$N) -NoNewline
}
Write-Host ''
}
if($Rock){
Write-Host $WriteRock -ForegroundColor Gray -BackgroundColor DarkGray
}
}
Demo -Funk -WriteFunk "Melt your brain"
Demo -Rock -WriteRock "Riders on the storm"
Thanks - that is awesome! And the code example gave me a smile!!! :)....Actually if i try and use that code snippet outside of the Function...i get an Ambigous Parameter Set error. Within the Function it is perfect
– blop001
Nov 19 '18 at 20:52
that would be because I switched from metal to rock and disco to funk halfway through writing it and forgot to switch all the variables, it's fixed now
– BaronW
Nov 19 '18 at 20:59
Thanks!!!! I have been able to adapt it successfully....well so far anyway. cheers
– blop001
Nov 19 '18 at 21:34
no problem, also note that you don't need to use the first argument as a dedicated switch, it is smart enough to know which set you are in by the variables that are used. I recommend investigating $PSCmdlet.ParameterSetName
– BaronW
Nov 19 '18 at 21:48
add a comment |
What you need are Parameter Sets
This is a demo with a function but it works just as well with a script (just put the param block at the top.)
function Demo {
param(
[Parameter(ParameterSetName='Funk')][switch]$Funk,
[Parameter(ParameterSetName='Rock')][switch]$Rock,
[Parameter(ParameterSetName='Funk')][string]$WriteFunk,
[Parameter(ParameterSetName='Rock')][string]$WriteRock
)
if($Funk){
foreach ($C in $WriteFunk.ToCharArray()){
$N = 0..15 | Get-Random
Write-Host $C -ForegroundColor $N -BackgroundColor $(15-$N) -NoNewline
}
Write-Host ''
}
if($Rock){
Write-Host $WriteRock -ForegroundColor Gray -BackgroundColor DarkGray
}
}
Demo -Funk -WriteFunk "Melt your brain"
Demo -Rock -WriteRock "Riders on the storm"
What you need are Parameter Sets
This is a demo with a function but it works just as well with a script (just put the param block at the top.)
function Demo {
param(
[Parameter(ParameterSetName='Funk')][switch]$Funk,
[Parameter(ParameterSetName='Rock')][switch]$Rock,
[Parameter(ParameterSetName='Funk')][string]$WriteFunk,
[Parameter(ParameterSetName='Rock')][string]$WriteRock
)
if($Funk){
foreach ($C in $WriteFunk.ToCharArray()){
$N = 0..15 | Get-Random
Write-Host $C -ForegroundColor $N -BackgroundColor $(15-$N) -NoNewline
}
Write-Host ''
}
if($Rock){
Write-Host $WriteRock -ForegroundColor Gray -BackgroundColor DarkGray
}
}
Demo -Funk -WriteFunk "Melt your brain"
Demo -Rock -WriteRock "Riders on the storm"
edited Nov 19 '18 at 20:50
answered Nov 19 '18 at 20:32


BaronWBaronW
1215
1215
Thanks - that is awesome! And the code example gave me a smile!!! :)....Actually if i try and use that code snippet outside of the Function...i get an Ambigous Parameter Set error. Within the Function it is perfect
– blop001
Nov 19 '18 at 20:52
that would be because I switched from metal to rock and disco to funk halfway through writing it and forgot to switch all the variables, it's fixed now
– BaronW
Nov 19 '18 at 20:59
Thanks!!!! I have been able to adapt it successfully....well so far anyway. cheers
– blop001
Nov 19 '18 at 21:34
no problem, also note that you don't need to use the first argument as a dedicated switch, it is smart enough to know which set you are in by the variables that are used. I recommend investigating $PSCmdlet.ParameterSetName
– BaronW
Nov 19 '18 at 21:48
add a comment |
Thanks - that is awesome! And the code example gave me a smile!!! :)....Actually if i try and use that code snippet outside of the Function...i get an Ambigous Parameter Set error. Within the Function it is perfect
– blop001
Nov 19 '18 at 20:52
that would be because I switched from metal to rock and disco to funk halfway through writing it and forgot to switch all the variables, it's fixed now
– BaronW
Nov 19 '18 at 20:59
Thanks!!!! I have been able to adapt it successfully....well so far anyway. cheers
– blop001
Nov 19 '18 at 21:34
no problem, also note that you don't need to use the first argument as a dedicated switch, it is smart enough to know which set you are in by the variables that are used. I recommend investigating $PSCmdlet.ParameterSetName
– BaronW
Nov 19 '18 at 21:48
Thanks - that is awesome! And the code example gave me a smile!!! :)....Actually if i try and use that code snippet outside of the Function...i get an Ambigous Parameter Set error. Within the Function it is perfect
– blop001
Nov 19 '18 at 20:52
Thanks - that is awesome! And the code example gave me a smile!!! :)....Actually if i try and use that code snippet outside of the Function...i get an Ambigous Parameter Set error. Within the Function it is perfect
– blop001
Nov 19 '18 at 20:52
that would be because I switched from metal to rock and disco to funk halfway through writing it and forgot to switch all the variables, it's fixed now
– BaronW
Nov 19 '18 at 20:59
that would be because I switched from metal to rock and disco to funk halfway through writing it and forgot to switch all the variables, it's fixed now
– BaronW
Nov 19 '18 at 20:59
Thanks!!!! I have been able to adapt it successfully....well so far anyway. cheers
– blop001
Nov 19 '18 at 21:34
Thanks!!!! I have been able to adapt it successfully....well so far anyway. cheers
– blop001
Nov 19 '18 at 21:34
no problem, also note that you don't need to use the first argument as a dedicated switch, it is smart enough to know which set you are in by the variables that are used. I recommend investigating $PSCmdlet.ParameterSetName
– BaronW
Nov 19 '18 at 21:48
no problem, also note that you don't need to use the first argument as a dedicated switch, it is smart enough to know which set you are in by the variables that are used. I recommend investigating $PSCmdlet.ParameterSetName
– BaronW
Nov 19 '18 at 21:48
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53381362%2fpowershell-parameters-command-line-menu%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
You are probably looking for parameter sets.
– Bill_Stewart
Nov 19 '18 at 19:33