I am attempting to pass the property of users account for thier supervisor then, perform an ad lookup on that id to return the supervisors ad account but it is failing.
Once i have the variable externalUser populated i then perform a lookup using the supervisorID such as this
get-aduser -filter {ldapID -like “$externalUser.supervisorID”} -properties name
When i press enter nothing is returned and no error appears (either in ise or powershell console)
if i fill $a = get-aduser -filter {ldapID -like “$externalUser.supervisorID”} -properties name
then perofrm $a.gettype() it returns You cannot call a method on a null-valued expression
I bet i am missing something easy but would love a nudge in the right direction please.
That’s a common gotcha when working with the AD cmdlets’ -Filter parameter. It actually takes a string rather than a ScriptBlock, and while all ScriptBlocks can be casted to strings, the results are sometimes not what you expect (particularly when trying to access the properties of variables, such as $externalUser.SupervisorID).
I’ve found that it helps to do one of two things:
# Either assign the value you want to compare directly to a variable, instead of needing a member expression:
$supervisorID = $externalUser.SupervisorID
Get-ADUser -Filter { ldapID -like $supervisorID } -Properties Name
# Or, just build a string and have complete control over how variable expansion is done:
Get-ADUser -Filter "ldapID -like '$($externalUser.supervisorID)'" -Properties Name
# (Note the single quotes around $($externalUser.supervisorID) )