I have this long line I want to make easier to read:
$Mail = "[email protected]"
Get-ADUser -Server example.com:3268 -Filter {EmailAddress -eq $Mail} -Properties CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress
I read that it's nice to use splatting, so I'm trying:
$Params = @{
Server = 'example.com:3268'
Filter = '{ EmailAddress -eq $Mail }'
Properties = 'CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress'
}
Get-ADUser @Params
But running this throws an error:
Get-ADUser : Error parsing query: '{ EmailAddress -eq [email protected] }' Error Message: 'syntax error' at position: '1'. At line:1 char:1 + Get-ADUser @Params + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
What am I missing?
You should pass a filter to the
ActiveDirectory
module cmdlets as astring
. You're needlessly including brackets in your code:While you can pass a
scriptblock
, it's implicitly being turned into astring
anyways. Additionally, you're passing your properties as a singlestring
when it's expecting anarray
ofstring
.The correct way:
I suggest checking
Get-Help
for parameter types: