Home > Software design >  Powershell Filter doesn't accept comma
Powershell Filter doesn't accept comma

Time:02-16

I try to use the Get-User command with a simple Filter.

Get-User -Filter "(Manager -eq 'Max, Mustermann')"

The problem is that i get this exception:

Cannot bind parameter 'Filter' to the target. Exception setting "Filter": "The value "Max, >Mustermann" could not be converted to type Microsoft.Exchange.Data.Directory.ADObjectId. "(Manager -eq 'Max, Mustermann')" at position 34." In C:\Users\JAKO\AppData\Local\Temp\tmp_4vtu0s13.ymv\tmp_4vtu0s13.ymv.psm1:38356 Zeichen:9

  •     $steppablePipeline.End()
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : WriteError: (:) [Get-User], ParameterBindingException
    • FullyQualifiedErrorId : >ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.GetUser

As far as I understand the Problem its because of the comma, so i tryed some workaorunds.

Get-User -Filter "(Manager -like 'Max, Mustermann')"

Here I dont get an exception but there are no Users that get returned.

Get-User -Filter "(Manager -eq 'Max"," Mustermann')"

The same as with the other workaround. No exception but no Users are Matching. I also made sure that i have Users that would match this specift query, by using this command

Get-User -Filter | Format-List Manager

How can I write my Filter input so it matches "Max, Mustermann"?

CodePudding user response:

From the filtering documentation for the Manager attribute:

This filter requires the distinguished name or canonical distinguished name of the manager (a mailbox or mail user). For example, Get-User -Filter "Manager -eq 'CN=Angela Gruber,CN=Users,DC=contoso,DC=com'" or Get-Mailbox -Filter "Manager -eq 'contoso.com/Users/Angela Gruber'". To find the distinguished name of a manager, replace with the name, alias, or email address of the recipient, and run this command: Get-Recipient -Identity "<RecipientIdentity>" | Format-List Name,DistinguishedName.

So now we know why the filter isn't working (a distinguished name is expected), and how to obtain the correct value (by using Get-Recipient):

# Fetch manager's user account object
$targetUser = Get-Recipient -Filter "SimpleDisplayName -eq 'Max, Mustermann'"

# Fetch reports 
Get-User -Filter "Manager -eq '$($targetUser.DistinguishedName)'"
  • Related