【发布时间】:2021-01-07 06:24:38
【问题描述】:
function test([string]$word){
return $word
}
Get-ADUser -Identity Function:\test("bill")
据我所知,上述内容应该可行。 MS在下面有一个示例,看起来他们将函数作为参数调用。但它不适合我。
MS 示例。
Get-ChildItem -Path Function:\Get-*Version |删除项目
链接 - https://docs.microsoft.com/en-us/powershell/scripting/learn/ps101/09-functions?view=powershell-7.1
【问题讨论】:
-
您需要将 func 及其参数用括号括起来,以强制 func 在将值传递给调用 func/cmdlet 之前进行评估。
-
顺便说一句:必须调用 PowerShell 函数、cmdlet、脚本和外部程序类似于 shell 命令 -
foo arg1 arg2- 不像 C#方法 -foo('arg1', 'arg2')。如果您使用,分隔参数,您将构造一个命令将其视为单个参数 的数组。为防止意外使用方法语法,请使用Set-StrictMode -Version 2或更高版本,但请注意其其他影响。请参阅this answer 了解更多信息。
标签: powershell