我正在创建一个 powershell 模块并遵循我导出的函数的动词-名词命名约定。
例如
function Do-Something() {
sleep 10
}
导入模块按预期工作......但是如果我将该模块作为自定义对象导入。
$myObject = Import-Module MyModule -AsCustomObject
然后尝试访问该函数,我得到一个解析错误:
PS > $myObject.Do-Something()
You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:22
+ $myObject.Do- <<<< Something()
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
我不明白。这是否意味着我不能在属于对象的方法中使用“-”?
我注意到 .NET 对象和其他 PSObject 的方法名称中没有“-”。
我也没有想出办法逃脱它……但即便如此,那也会很困惑。
最佳答案
试试这个:
$myObject."Do-Something"()
关于powershell - powershell 模块中正确的函数名称约定是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6507068/