powershell parameters command line menu












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










share|improve this question






















  • You are probably looking for parameter sets.
    – Bill_Stewart
    Nov 19 '18 at 19:33
















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










share|improve this question






















  • You are probably looking for parameter sets.
    – Bill_Stewart
    Nov 19 '18 at 19:33














0












0








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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












1 Answer
1






active

oldest

votes


















3














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"





share|improve this answer























  • 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













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
});


}
});














draft saved

draft discarded


















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









3














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"





share|improve this answer























  • 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


















3














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"





share|improve this answer























  • 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
















3












3








3






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"





share|improve this answer














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"






share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory