powershell - 在powershell cmdlet中调用函数作为参数
问题描述
function test([string]$word){
return $word
}
Get-ADUser -Identity Function:\test("bill")
据我所知,上述内容应该有效。MS在下面有一个示例,看起来他们将函数作为参数调用。但它不适合我。
微软的例子。
Get-ChildItem -Path Function:\Get-*Version | 除去项目
链接 - https://docs.microsoft.com/en-us/powershell/scripting/learn/ps101/09-functions?view=powershell-7.1
解决方案
要将命令的 / 表达式的输出作为命令参数传递,请使用(...)
分组运算符:
Get-ADUser -Identity (test "bill")
至于Function:\test
语法:只能用于检索函数定义,不能用于直接调用。
该语法依赖于Function:
PowerShell 驱动器,它提供对所有函数定义的访问 - 请参阅about_Providers概念帮助主题。
为了完整起见(这里没有充分的理由采用这种方法):使用命名空间变量表示法和调用运算符&
,您可以利用路径语法 - 尽管没有- 如下:Function:\test
\
# Same as above.
Get-ADUser -Identity (& $Function:test "bill")
推荐阅读
- database - 与以前的混合模型相比,Aerospike 中的 sprigs 是否减少了主索引的内存占用?
- mule - Dataweave 1.0 检查数组大小
- xml - 我可以在 XSLT 1.0 中创建子元素计数器吗?
- .net - Net Core WCF 错误“发生一个或多个错误。(没有端点监听”
- javascript - 我从 class.length 得到错误的长度
- c# - c# regex.replace 需要替换3个或更多空格
- java - 为什么我的列表需要会话中的空列表才能显示添加到该列表中的所有项目?
- c# - 如何通过组织身份验证从 Blazor Server 中的身份获取用户属性
- c# - SystemWrapper 和 Castle Windsor 的问题
- mongodb - MongoDB 允许多个加入和离开日期