In this short post I would like to share one interesting feature of the PowerShell: parameters matching by partial name. Suppose that you have the test.ps1 script with switch parameter:
1: param(
2: [switch]$recreateSite
3: )
4:
5: if ($recreateSite)
6: {
7: Write-Host "recreateSite"
8: }
If you run it in PowerShell console like this:
1: .\test.ps1 –recreateSite
it will output “recreateSite” in console. Now the interesting thing is that you can run it like this:
1: .\test.ps1 –r
and result will be the same. I.e. it is not necessary to specify whole name for the parameter – PowerShell will find closest match (-r corresponds to -recreateSite).
Let’s add another parameter “recreateDb”:
1: param(
2: [switch]$recreateSite,
3: [switch]$recreateDb
4: )
5:
6: if ($recreateSite)
7: {
8: Write-Host "recreateSite"
9: }
10:
11: if ($recreateDb)
12: {
13: Write-Host "recreateDb"
14: }
If you now will run it like this:
1: .\test.ps1 –r
you will see the following error:
Parameter cannot be processed because the parameter name 'r' is ambiguous. Possible matches include: -recreateSite -recreateDb.
Now in order to run it you will need to use minimal name which will uniquely match some parameter. In our example:
1: .\test.ps1 -recreates
will correspond to “recreateSite”. And:
1: .\test.ps1 -recreated
will match to “recreateDb”.
No comments:
Post a Comment