Powershell variables in Get-AdUser

混江龙づ霸主 提交于 2019-12-25 12:36:32

问题


I'm trying to write a simple powershell script.

Get-ADuser -Filter {GivenName -eq $GivenName $hateList} -SearchBase $Container -Properties displayName,telephoneNumber,department|ForEach-Object {"FullName`t: $($_.displayName)`r`nPhone`t`t: $($_.telephoneNumber)`r`nDepartment`t: $($_.department)`r`n"}

The error what I got: Get-ADUser : Error parsing query: 'GivenName -eq $GivenName $hateList' Error Message: 'syntax error' at position: '26'.

So the problem is that the variables aren't substituted with their values. What do I do wrong?


回答1:


You Filter parameter is not right. If you want to have GivenName be equal to $GivenName you should do it like this:

{GivenName -eq $GivenName}

If you want it to be equal to $GivenName or $hateList, whatever it is, you should try something like:

{(GivenName -eq $GivenName) -or (GivenName -eq $hateList)}

Check this link for more filter: http://technet.microsoft.com/en-us/library/ee617241.aspx

You can first get list of users with filter like this:

{GivenName -eq $GivenName}

And the do some post processing:

$users | Where-Object { $hateList -notcontains  $_.cn }



回答2:


If variable expansion doesn't work when passing the variable, try to pass it enclosed in quotes:

{GivenName -eq "$GivenName"}


来源:https://stackoverflow.com/questions/9680504/powershell-variables-in-get-aduser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!