我想为我自己的函数重新使用一个已经存在的枚举。我想要来自 Test-Path -PathType
的枚举。比如,我想要我自己的函数,它有一个接受Any
、Leaf
或Container
参数的PathType
。
这可以在不重新定义枚举的情况下完成吗?我如何确定枚举本身的实际名称?
最佳答案
这是TestPathType枚举。你应该能够接受它作为一个正式的论点,例如:
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
关于Powershell:我可以将现有枚举用作我自己函数的参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966219/