Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 在函数名中实现的变量_Function_Powershell_Variables_Active Directory - Fatal编程技术网

Function 在函数名中实现的变量

Function 在函数名中实现的变量,function,powershell,variables,active-directory,Function,Powershell,Variables,Active Directory,由于我在任何地方都找不到解决问题的方法,我将在这里询问: 基本上,我正在尝试为AD创建一个基本函数,其中函数名中有一个变量-在我的例子中,是这样的: "Get-AD$FilterItem" 其中,$FilterItem将是用户、计算机、组等,因此我不必为每个广告项都使用相同的功能 但我不能让它工作,它总是给我 …:术语“get ad$filteritem”无法识别 怎么办?快速回复 $i="user" $f="get-ad$i" &$f username 快速回复 $i="user"

由于我在任何地方都找不到解决问题的方法,我将在这里询问: 基本上,我正在尝试为AD创建一个基本函数,其中函数名中有一个变量-在我的例子中,是这样的:

"Get-AD$FilterItem"
其中,
$FilterItem
将是用户、计算机、组等,因此我不必为每个广告项都使用相同的功能

但我不能让它工作,它总是给我

…:术语“get ad$filteritem”无法识别

怎么办?

快速回复

$i="user"
$f="get-ad$i"
&$f username
快速回复

$i="user"
$f="get-ad$i"
&$f username

您可以为函数提供程序创建
新项
。您最好也创建一个闭包,而不是简单的脚本块,这样您就可以在函数中使用该值:

$x = "Foo"
New-Item -Path Function: -Name "Shout$x" -value { write-host "$x" }.GetNewClosure()
$x = "Bar"
New-Item -Path Function: -Name "Shout$x" -value { write-host "$x" }.GetNewClosure()
ShoutFoo
ShoutBar

您可以为函数提供程序创建
新项
。您最好也创建一个闭包,而不是简单的脚本块,这样您就可以在函数中使用该值:

$x = "Foo"
New-Item -Path Function: -Name "Shout$x" -value { write-host "$x" }.GetNewClosure()
$x = "Bar"
New-Item -Path Function: -Name "Shout$x" -value { write-host "$x" }.GetNewClosure()
ShoutFoo
ShoutBar

大家好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。这看起来非常像一个。请退后一步,描述您试图解决的实际问题,而不是您认为的解决方案。你认为你需要这个做什么?你好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。这看起来非常像一个。请退后一步,描述您试图解决的实际问题,而不是您认为的解决方案。你觉得你需要这个干什么?谢谢,不知为什么我没有想到简单的电话