带有破折号的参考命令名称

Pet*_*voy 7 powershell powershell-2.0

我最近发现Powershell函数只是命名为scriptblocks.例如

function HelloWorld {
    Write-Output "Hello world"
}

$hw = $function:HelloWorld

& $hw     
Run Code Online (Sandbox Code Playgroud)

将执行HelloWorld方法.

但是,我无法弄清楚的是,如何获得对其名称中包含破折号的方法的引用:

function Hello-World {
    Write-Output "Hello world"
}

$hw = $function:Hello-World

You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:27
+     $hw = $function:Hello- <<<< World
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

我知道我可以这样做:

$hw = (Get-Item function:Hello-World).ScriptBlock
Run Code Online (Sandbox Code Playgroud)

但它有点"嘈杂",我喜欢$ function语法

Pet*_*voy 5

哦!我应该坚持程序员问题解决序列,我发布到 SO之前询问我的同事。看起来我应该使用:

$hw = ${function:Hello-World}
Run Code Online (Sandbox Code Playgroud)