0
votes

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?

2

2 Answers

1
votes

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 }
0
votes

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

{GivenName -eq "$GivenName"}