Powershell:我可以为自己的函数使用现有枚举吗';s参数?

Powershell:我可以为自己的函数使用现有枚举吗';s参数?,powershell,parameters,enums,Powershell,Parameters,Enums,我想为我自己的函数重新使用已经存在的枚举。我想要测试路径-PathType中的枚举。与中一样,我希望我自己的函数具有一个参数PathType,该参数接受Any、Leaf或容器 这可以在不重新定义枚举的情况下完成吗?如何确定枚举本身的实际名称?它是枚举。你应该能够接受它作为一个正式的论点,例如: function Do-Something([Microsoft.PowerShell.Commands.TestPathType] $pathType) { ... } 顺便说一下,当您提供无效值

我想为我自己的函数重新使用已经存在的枚举。我想要
测试路径-PathType
中的枚举。与中一样,我希望我自己的函数具有一个参数
PathType
,该参数接受
Any
Leaf
容器

这可以在不重新定义枚举的情况下完成吗?如何确定枚举本身的实际名称?

它是枚举。你应该能够接受它作为一个正式的论点,例如:

function Do-Something([Microsoft.PowerShell.Commands.TestPathType] $pathType) {
  ...
}
顺便说一下,当您提供无效值时,Powershell将在错误消息中告诉您参数的类型,例如:

Test-Path -PathType Banana
收益率:

Test-Path : Cannot bind parameter 'PathType'. Cannot convert value "Banana" to type
"Microsoft.PowerShell.Commands.TestPathType". Error: "Unable to match the identifier name Banana to a valid enumerator
name.  Specify one of the following enumerator names and try again: Any, Container, Leaf"
At line:1 char:21
但是,如果您想更主动地查看命令元数据,可以直接检查它,例如:

(Get-Command Test-Path).Parameters.PathType.ParameterType

非常感谢。在您向我展示(Get Command Test Path)之前,我无法找到“获取”单个参数的方法。parameters看起来TestPathType枚举在PowerShell 6中消失了。