繁体   English   中英

在 powershell cmdlet 中调用 function 作为参数

[英]Calling function as parameter in powershell cmdlet

function test([string]$word){
return $word
}

Get-ADUser -Identity Function:\test("bill")

据我所知,上述内容应该有效。 MS 在下面有一个示例,看起来他们将 function 作为参数调用。 但它不适合我。

微软的例子。

Get-ChildItem-Path Function:\Get-*Version | 除去项目

链接 - https://docs.microsoft.com/en-us/powershell/scripting/learn/ps101/09-functions?view=powershell-7.1

要将命令的 / 表达式的 output 作为命令参数传递,请使用(...) 分组运算符

Get-ADUser -Identity (test "bill")

至于Function:\test语法:只能用于检索 function定义,不能用于直接调用

该语法依赖于Function: PowerShell 驱动器,它提供对所有 function 定义的访问 - 请参阅about_Providers概念帮助主题。

为了完整起见(这里没有充分的理由采用这种方法):使用命名空间变量表示法&调用运算符,您可以利用Function:\test路径语法 - 尽管没有\ - 如下:

# Same as above.
Get-ADUser -Identity  (& $Function:test "bill")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM