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)]
- $ServerPath
- )
- "You selected $ServerPath"
- }
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)]
- $ComputerName
- )
- "You selected $ComputerName"
- }
Old code can now still run, and new code (and code completion) will use the new name:复制代码
- function Test-Function
- {
- param
- (
- [Parameter(Mandatory=$true)]
- [Alias("ServerPath")]
- $ComputerName
- )
- "You selected $ComputerName"
- }
PS> Test-Function -ComputerName server1 You selected server1 PS> Test-Function -ServerPath server1 You selected server1 |
复制代码
- [Alias("ServerPath","CN")]
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |