@JaredBusch said in Need a good example of getting powershell arguments:
I'll hit the google later, because I am on other things, but I found that something I touched today could very easily be improved if I can add parameter handling to the powershell script.
Now, the basics are easy as it is all in the $ARGS variable/object.
But I want to have some safety checking. because it is easier to do things right the first time.
Example: I want a parameter to note if I should make the thing being done the default.
I can pass a 1 like dothing.ps1 1 and I can simply code something to check $ARG[0] eq "1" but that is not very explanatory to the person using the script.
This is more explanatory dothing.ps1 -default for a command.
So has anyone seen a good example of parameter handling that I can put into my dothing.ps1 script?
I'm not sure I understand exactly what you mean.
Taking a guess here, but how I understand is that you'd want to add this at the top of your script:
[cmdletbinding()]
param (
[Parameter()]
[Switch]$Default
)
if ($Default) {
Write-Host "The -Default parameter was specified."
} else {
Write-Host "The -Default parameter was NOT specified."
}
Doing that will give you the following output:
PS > .\JBTest.ps1 -Default
The -Default parameter was specified.
PS > .\JBTest.ps1
The -Default parameter was NOT specified.
If you want to accept input from a pipeline to work with, let me know.