powershell - powershell 模块中正确的函数名称约定是什么?

标签 powershell

我正在创建一个 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/

相关文章:

powershell - 退出 powershell 脚本后,通过 New-PSDrive 映射的网络驱动器消失

sql - 在Powershell变量/数组中获取特定行

powershell - $PSCmdlet.ShouldProcess 状态在调用之间存储在哪里(以及如何重置)?

powershell - 将 -Verbose 状态传递给模块 cmdlet

powershell - 如何使用MOF创建自定义DSC资源

regex - 在 powershell 选择字符串输出中删除以 *(asterisk) 开头的行

powershell - 使用 Azure powershell 列出容器上的现有 SASToken

powershell - 为所有构建定义设置构建模板

powershell - 尝试通过 Powershell 脚本在 IIS 中创建网站

powershell - 查找名称相似的文件,如果存在,则使用 PowerShell 删除不带特定字符串的文件