If I define the following
[parameter(Mandatory = $true)]
[alias("db")]
[string]$database,
then I get an error
Parameter alias cannot be specified because an alias with the name 'db'
was defined multiple times for the command.
Which is true, since db
is already an alias for the universal -Debug
parameter.
Is it possible to define this alias without renaming the parameter?
You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don't need to pass the argument because the variable is itself a Public and can be accessible inside the function.
PowerShell includes built-in aliases that are available in each PowerShell session. The Get-Alias cmdlet displays the aliases available in a PowerShell session. To create an alias, use the cmdlets Set-Alias or New-Alias . In PowerShell 6, to delete an alias, use the Remove-Alias cmdlet.
An alias is an alternate name or nickname for a cmdlet or for a command element, such as a function, script, file, or executable file. You can use the alias instead of the command name in any PowerShell commands.
New-Alias (Microsoft.PowerShell.Utility) - PowerShell The New-Alias cmdlet creates a new alias in the current PowerShell session. Aliases created by using New-Alias are not saved after you exit the session or close PowerShell. You can use the Export-Alias cmdlet to save your alias information to a file.
Sorry, you can't. -Debug
is a common parameter, so -Debug
and -db
are switches that are available on pretty much everything including the functions you write yourself. As the error tells you, it's already defined.
Even if it were possible to go around undefining built-in aliases, that unexpectantly changes the meaning of a call like test-db -db
for someone else who frequently uses -db
instead of -Debug
. They expect it to enable debugging output, not specify a different parameter.
Consider this function:
function test-db{
param(
[parameter(mandatory=$true)]
[string]$database)
write-host 'database' $database
write-debug 'debugging output'
}
Now call it with test-db server
, test-db -db server
, and test-db server -db
. The first doesn't do write-debug
, while the other 2 do, no matter where -db
is. You also can't define a separate parameter [string]$db
(or rename $database
to $db
) because Powershell will give you this error:
Parameter 'db' cannot be specified because it conflicts with the parameter alias of the same name for parameter 'Debug'.
More info on this, per MSDN:
In addition to using the AliasAttribute attribute, the Windows PowerShell runtime performs partial name matching, even if no aliases are specified. For example, if your cmdlet has a FileName parameter and that is the only parameter that starts with F, the user could enter Filename, Filenam, File, Fi, or F and still recognize the entry as the FileName parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With