Trying to filter the Get-ADUser results by multiple properties, or a single property, determined by a single previously set variable. Below are the two pieces cut from the script
# Target users with Active Directory user object properties
$PROPERTY_NAME = 'Surname', 'GivenName'
$PROPERTY_VALUE = 'Franco', 'Alex'
Collect all Acitve Directory users matching the property/properties defined above
Get-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Properties'. Specified method is not supported.
I'd rather not have to do something like
if(PROPERTY_NAME.GetType().Name -eq 'Object[]') {
$num = $PROPERTY_NAME.Count
$num | % {
Get-ADUser -Filter {$PROPERTY_NAME[($_ - 1)] -eq $PROPERTY_VALUE[($_ - 1)]
}
# haven't worked out yet what to do from this point, but I'd rather not have to anyways
}
Is there a more elegant way to Get-ADUser with multiple property filters?
Using the Get-Help Cmdlet provides the key to your answer here, it really boils down to a couple of things. First, what time of input is the parameter expecting (in this case String) and what sort of syntax is expected. You have the choice of a couple of different parameters. -Filter and -LDAPFilter, depending on if you want to use the native PS query syntax or LDAP. If we are working with the -Filter for example we need to group each criteria into separate parentheses’ see below for an example
Wow, that guy hates using script blocks for filters but has valid points. Had some trouble getting it working with the @params, but was able to get it after a brain break and seeing Logan’s example.