PowerShell的AD模块 - 在筛选变量 [英] PowerShell AD Module - Variables in Filter
问题描述
对不起,我简单的问题开始,但我难倒了答案。
Sorry for the simple question to start, but I am stumped on the answer.
我的code是简单的......我想利用命令行可变进我的脚本,并使用该变量作为AD命令中的过滤字符串。我有如下:
My code is simple... I want to take a variable from command line into my script and use that variable as a Filter string within an AD command. I have as follows:
PARAM($myOU)
$FoundOUs = Get-ADOrganizationalUnit -Filter 'Name -like "*"' -SearchBase ="OU=Offices,DC=dc1,DC=domain,DC=com"
所以,我想替换*与 $ myOU ...我是在失去了如何做到这一点。我试图像 -Filter名称$ myOU 等,但没有运气。任何建议将是巨大的。
So, I want to replace "*" with $myOU... I am at a lost on how to do this. I have tried things like -Filter Name $myOU, etc, but no luck. Any suggestions would be great.
推荐答案
使用字符串插值像这样:
Use string interpolation like so:
$FoundOUs = Get-ADOrganizationalUnit -Filter "Name -like '$myOU'" -SearchBase="OU=Offices,DC=dc1,DC=domain,DC=com"
请注意该字符串插值只能用双引号中的字符串发生这样换单和功放的顺序;双引号这样的变量将被插入。同时使用 $($ myOU)
是不必要在这种情况下。您通常使用一个子前pression当你需要访问一个属性,例如 $($ myOU.Length)
或一般评价一个字符串中的前pression。
Note that string interpolation only happens with double quoted strings so swap the order of single & double quotes so the variable will be interpolated. Also using $($myOU)
is unnecessary in this case. You typically use a sub-expression when you need to access a property e.g. $($myOU.Length)
or in general evaluate an expression inside a string.
这篇关于PowerShell的AD模块 - 在筛选变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!