Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a script with parameter that has [alias("db")] in Powershell?

Tags:

powershell

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?

like image 526
Andrey Shchekin Avatar asked Jul 29 '11 08:07

Andrey Shchekin


People also ask

How do you pass parameters to a PowerShell script?

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.

How do I create an alias in PowerShell?

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.

What are alias commands in PowerShell?

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.

What is the cmdlet used to create alias?

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.


1 Answers

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.

like image 159
Joel B Fant Avatar answered Nov 02 '22 06:11

Joel B Fant