PowerShell可以';是否不使用匹配的枚举类型?
我在做什么蠢事吗 我指定函数采用特定的枚举类型作为参数:PowerShell可以';是否不使用匹配的枚举类型?,powershell,types,parameters,enums,Powershell,Types,Parameters,Enums,我在做什么蠢事吗 我指定函数采用特定的枚举类型作为参数: PS> add-type -AssemblyName System.ServiceProcess PS> function test([System.ServiceProcess.ServiceControllerStatus]$x) { Write-host $x $x.gettype() } 由于我可以访问该类型的实例(并且我手动导入了该程序集),因此该类型在范围内最为明确: 然后,当我尝试向函数传递所述枚举的实例时,它
PS> add-type -AssemblyName System.ServiceProcess
PS> function test([System.ServiceProcess.ServiceControllerStatus]$x) { Write-host $x $x.gettype() }
由于我可以访问该类型的实例(并且我手动导入了该程序集),因此该类型在范围内最为明确:
然后,当我尝试向函数传递所述枚举的实例时,它会出错:
PS> test [System.ServiceProcess.ServiceControllerStatus]::Stopped
test : Cannot process argument transformation on parameter 'x'. Cannot convert value
"[System.ServiceProcess.ServiceControllerStatus]::Stopped" to type "System.ServiceProcess.ServiceControllerStatus".
Error: "Unable to match the identifier name [System.ServiceProcess.ServiceControllerStatus]::Stopped to a valid
enumerator name. Specify one of the following enumerator names and try again: Stopped, StartPending, StopPending,
Running, ContinuePending, PausePending, Paused"
At line:1 char:6
+ test [System.ServiceProcess.ServiceControllerStatus]::Stopped
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [test], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,test
不过,强制使用一个字符串是非常愉快的:
PS> test 'Stopped'
Stopped System.ServiceProcess.ServiceControllerStatus
发生了什么事?显然,PowerShell认为我发送的是字符串,而不是枚举对象。如果引用完全限定名,则会收到相同的错误消息:
PS> test '[System.ServiceProcess.ServiceControllerStatus]::Stopped'
test : Cannot process argument transformation on parameter 'x'. Cannot convert value
"[System.ServiceProcess.ServiceControllerStatus]::Stopped" to type "System.ServiceProcess.ServiceControllerStatus".
Error: "Unable to match the identifier name [System.ServiceProcess.ServiceControllerStatus]::Stopped to a valid
enumerator name. Specify one of the following enumerator names and try again: Stopped, StartPending, StopPending,
Running, ContinuePending, PausePending, Paused"
At line:1 char:6
+ test '[System.ServiceProcess.ServiceControllerStatus]::Stopped'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [test], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,test
将枚举放在括号中以强制PowerShell首先对其求值,这样做的技巧如下:
PS> test ([System.ServiceProcess.ServiceControllerStatus]::Stopped)
Stopped System.ServiceProcess.ServiceControllerStatus
关于解析模式,您遇到了一个小问题。你可以在论点周围加上括号,它会起作用:
test ([System.ServiceProcess.ServiceControllerStatus]::Stopped)
或者,从字符串到枚举的转换自然发生,因此您可以编写:
test Stopped
以下是一些讨论解析模式的好链接:
PS> test Stopped
Stopped System.ServiceProcess.ServiceControllerStatus
也就是说,当我调用.NET方法时,我更喜欢使用完全限定的枚举值,而不是字符串。这是因为.NET方法往往具有多个重载,而那些采用字符串的方法在选择正确的重载时可能会混淆PowerShell。您没有正确指定
$x
参数。它需要包装在param()
表达式中
function test
{
param(
[ServiceProcess.ServiceControllerStatus]
$x
)
Write-host $x $x.gettype()
}
另外,您可以省略
系统。
任何类型名称的部分。非常确定这是PS1时代的老建议。。。如其他答案所示,问题中的语法与预期一样有效。正是这个电话遇到了一些解析问题。
function test
{
param(
[ServiceProcess.ServiceControllerStatus]
$x
)
Write-host $x $x.gettype()
}