Let's assume you have created this function:- function Test-Function
- {
- param
- (
- [Parameter(Mandatory=$true)]
- $ServerPath
- )
-
- "You selected $ServerPath"
- }
复制代码 It works fine, but half a year later in a code review, your boss wants you to use standard parameter names, and rename "ServerPath" to "ComputerName". So you change your function appropriately:- function Test-Function
- {
- param
- (
- [Parameter(Mandatory=$true)]
- $ComputerName
- )
-
- "You selected $ComputerName"
- }
复制代码 What you cannot easily control, though, is who else calls your function, and may still use the old parameter. So to ensure backward compatibility, make sure your function can still work with the old parameter name, too:- function Test-Function
- {
- param
- (
- [Parameter(Mandatory=$true)]
- [Alias("ServerPath")]
- $ComputerName
- )
-
- "You selected $ComputerName"
- }
复制代码 Old code can now still run, and new code (and code completion) will use the new name:
PS> Test-Function -ComputerName server1
You selected server1
PS> Test-Function -ServerPath server1
You selected server1
|
http://powershell.com/cs/blogs/tips/archive/2014/02/12/ensuring-backward-compatibility.aspx |