简体   繁体   English

具有多个过滤器和变量的Get-ADUser

[英]Get-ADUser with multiple filters & variables

I'm trying to get AD users into a variable using multiple filters. 我正在尝试使用多个过滤器使AD用户进入变量。 However one of the filters has variables in it & I can't get it to work... I have searched for similar issues & tried applying those but nothing seems to work. 但是,其中一个过滤器中包含变量,并且我无法使其正常工作。我已经搜索了类似的问题并尝试应用这些问题,但似乎没有任何效果。

$FilterBase = "department"
$Filter = "IT"

$ADusers = Get-ADUser -ResultSetSize $null -SearchBase "OU=Users,DC=mydomain,DC=com" -Properties * -Filter {(Enabled -eq $True) -and ($FilterBase -like $Filter) -and (cn -notlike ""SMB_*"")} |
            Select-Object distinguishedName |
            Sort-Object distinguishedName

I'm trying to fill $ADusers with all enabled users whose commonname doesn't start with "SMB_" (don't ask) & where the department is IT. 我正在尝试使用所有启用的用户填充$ADusers ,这些用户的通用名称都不以“ SMB_”开头(不要问)以及部门位于IT部门。 I used -like to prevent issues if the values in AD would have different casings (uppercase, lowercase, mixed case, ...). 我使用了-like来防止问题,如果AD中的值具有不同的大小写(大写,小写,混合大小写...)。

The reason that I'm using variables for this is because in the end the script will be dynamic. 我为此使用变量的原因是,最终脚本将是动态的。 At some point $FilterBase is going to be "company" instead of "department" and $Filter is going to be "HR" instead of "IT" etc... 在某些时候, $FilterBase将是“公司”而不是“部门”,而$Filter将是“ HR”而不是“ IT”等。

But I just can't seem to get it to work: 但我似乎无法使其正常工作:

 Get-ADUser : Error parsing query: '(Enabled -eq $True) -and ($FilterBase -like $Filter) -and (cn -notlike ""SMB_*"")' Error Message: 'syntax error' at position: '74'.
    At line:4 char:12

I have tried using quotes around the variables like " $Filter ", " $($Filter) ", ' $Filter ' but alas. 我试过在变量“ $Filter ”,“ $($Filter) ”,“ $Filter ”周围使用引号,但是可惜。 And I know it's not best practice to use variables in Filter but I can't think of any other way to accomplish this. 而且我知道在Filter中使用变量不是最佳实践,但我想不出任何其他方法来实现此目的。

Any suggestions? 有什么建议么?

its just simply syntax error. 它只是简单的语法错误。

$enabled = 'Enabled'
$EnabledTrueOrFalse = $true
$SN = 'Surname'
$surname = "Doe"
$OU = "OU=Users,DC=mydomain,DC=com"

Get-ADuser -filter{$enabled -eq $EnabledTrueOrFalse -and $SN -eq $surname} -SearchBase $OU -Properties * | Select-Object distinguishedName | Sort-Object distinguishedName

read more about it here 在这里阅读更多关于它的信息

Thanks for the tips guys. 谢谢提醒伙计。 I couldn't get it to work with multiple filters so I moved some filters to the where clause. 我无法使其与多个过滤器一起使用,因此将一些过滤器移至where子句。

My current (working) code is now: 我当前(工作)的代码现在是:

$FilterBase = "department"
$Filter = "IT"

$ADusers = Get-ADUser -ResultSetSize $null -SearchBase "OU=Users,DC=mydomain,DC=com" -Properties * -Filter "$FilterBase -like `"$Filter`"" |
Where {$_.Enabled -eq $True -and $_.CN -notlike "SMB_*"} |
Select-Object distinguishedName |
Sort-Object distinguishedName

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

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