在PowerShell中重载函数 [英] Function overloading in PowerShell
问题描述
我想让我的函数接受一个字符串,数组或一些开关。
一个我想要的例子:
'Joe')
在PowerShell函数中不会重载。最后一个定义覆盖同一范围内的前一个定义或隐藏父范围中的前一个定义。因此,您应该创建一个函数并提供一种方法来通过参数区分其调用模式。
在V2中,您可以使用高级函数,请参阅 help about_Functions_Advanced_Parameters
并避免在解决参数集模糊度方面进行一些手动编码:
#advanced function with 3参数集
函数备份用户数据
(
[Parameter(Position = 0,ParameterSetName =user)]
[string] $ user,
[Parameter Position = 0,ParameterSetName =array)]
[object] []] $ array,
[Parameter(Position = 0,ParameterSetName =all)]
[switch] $ all
)
{
#使用它获取参数集名称
$ PSCmdlet.ParameterSetName
}
#test
Backup-UsersData -user'John'
Backup-UsersData 1,2
Backup-UsersData -all
#OUTPUT:
#user
#array
#all
请注意,这种机制有时很奇怪。例如,在第一个测试中,我们必须明确指定参数名称 -user
。否则:
Backup-UsersData:无法使用指定的命名参数解析参数集。
$ p $
在C:\ TEMP \_101015_110059 \try2.ps1:21 char:17
+ Backup-UsersData << 'John'
+ CategoryInfo:InvalidArgument:(:) [Backup-UsersData],ParentContainsErrorRecordException
+ FullyQualifiedErrorId:AmbiguousParameterSet,Backup-UsersData
$ b function Backup-UsersData
(
[string] $ user,
[object]] $ array,
[switch] $ all
)$ b $ ($ user){'user'}
elseif($ array){'array'}
elseif($ all){'all'}
else { '可能'}
}
Backup-UsersData -user'John'
Backup-UsersData -array 1,2
Backup-UsersData -all
备份用户数据
但是在这种情况下,您应该解决(或接受并忽略)歧义,例如决定该怎么做,比如说:
pre $ Backup-UsersData -user'John'-array 1,2 -all
Can you overload functions in PowerShell?
I want to my function to accept a string, array or some switch.
An example of what I want:
- Backup-UsersData singleUser
- Backup-UsersData @('Alice', 'Bob', 'Joe')
- Backup-UsersData -all
In PowerShell functions are not overloaded. The last definition overrides the previous in the same scope or hides the previous in a parent scope. Thus, you should create a single function and provide a way to distinguish its call mode by arguments.
In V2 you may use an advanced function, see help about_Functions_Advanced_Parameters
and avoid some manual coding on resolving parameter set ambiguities:
# advanced function with 3 parameter sets
function Backup-UsersData
(
[Parameter(Position=0, ParameterSetName="user")]
[string]$user,
[Parameter(Position=0, ParameterSetName="array")]
[object[]]$array,
[Parameter(Position=0, ParameterSetName="all")]
[switch]$all
)
{
# use this to get the parameter set name
$PSCmdlet.ParameterSetName
}
# test
Backup-UsersData -user 'John'
Backup-UsersData 1, 2
Backup-UsersData -all
# OUTPUT:
# user
# array
# all
Note that this mechanism is sometimes strange. For example in the first test we have to specify parameter name -user
explicitly. Otherwise:
Backup-UsersData : Parameter set cannot be resolved using the specified named parameters.
At C:\TEMP\_101015_110059\try2.ps1:21 char:17
+ Backup-UsersData <<<< 'John'
+ CategoryInfo : InvalidArgument: (:) [Backup-UsersData], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Backup-UsersData
In many cases standard, not advanced, function with mixed parameters will do:
function Backup-UsersData
(
[string]$user,
[object[]]$array,
[switch]$all
)
{
if ($user) {'user'}
elseif ($array) {'array'}
elseif ($all) {'all'}
else {'may be'}
}
Backup-UsersData -user 'John'
Backup-UsersData -array 1, 2
Backup-UsersData -all
Backup-UsersData
But in this case you should resolve (or accept and ignore) ambiguities, e.g. to decide what to do if, say:
Backup-UsersData -user 'John' -array 1, 2 -all
这篇关于在PowerShell中重载函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!