Powershell:我可以将现有枚举用作我自己函数的参数吗?

标签 powershell parameters enums

我想为我自己的函数重新使用一个已经存在的枚举。我想要来自 Test-Path -PathType 的枚举。比如,我想要我自己的函数,它有一个接受AnyLeafContainer 参数的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/

相关文章:

powershell - PowerShell null不会被传递

javascript - 使用 Promise.all().then() 动态设置参数

java - 是否可以在枚举中转义 ": "冒号字符?

ios - 枚举类型的开关大小写

java - 如何使用参数 Wicket 引用页面

php - 在 MYSQLi 中插入 'ENUM'

powershell - 对函数参数中声明的不存在文件进行强类型检查

arrays - Powershell-如何用数组列表值替换查询结果值

powershell - 如何从 Powershell 打开 Powershell 控制台窗口

ruby-on-rails - 两个参数的值未显示 ruby​​ on rails