0

I'm getting a bit confused here. I'm probably just missing something this late at night. I have issues with a bit of my code. See the below code.

$MsolUser = Get-MsolUser -UserPrincipalName name@contonso.com
Get-ADUser -filter {emailaddress -Like "$($MsolUser.UserPrincipalName)"} -Properties EmailAddress, LastLogonDate, SamAccountName | select EmailAddress, LastLogonDate, SamAccountName

What the above portion should do, is collect the userprincipalname of the MsolUser from the variable $MsolUser, search for a corresponding emailaddress in AD and return the EmailAddress, LastLogonDate and SamAccountName.

If I try to fetch the value through the variable like above, it doesn't return what it should. It does not give me any errors. I know I have done something like this before, but I just can't wrap my head around it at the moment.

Appreciate the help!

Thanks in advance!

CraCra
  • 1
  • 2
  • Possible duplicate of [Get-Aduser -Filter will not accept a variable](https://stackoverflow.com/questions/20075502/get-aduser-filter-will-not-accept-a-variable) – BenH Nov 10 '17 at 21:13
  • Use `Get-ADUser -filter "emailaddress -Like '$($MsolUser.UserPrincipalName)'"` instead. Double quote outer, single quote inner as a general rule with the AD cmdlet filters. mklement0's answer in the linked duplicate gives the details on why. – BenH Nov 10 '17 at 21:20
  • That's what I did do at first, but it still doesn't return the fetched value in $MsolUser, that's what makes me confused. There's no problem at all displaying the value if you fetch the value straight out of the variable: $MsolUser.UserPrincipalName like it should, but no matter how I try to collect this data in my Get-ADUser string, it won't work. – CraCra Nov 10 '17 at 21:54

1 Answers1

0

Seems like I was tired when I sat down and did this. The code worked just fine together with the variable if I had just used it as it was.

See below for the working corrected version:

$msoluser = get-msoluser -userprincipalname name@cotonso.com
$aduser = Get-ADUser -filter { emailaddress -Like $msoluser.UserPrincipalName} -Properties EmailAddress, LastLogonDate, SamAccountName | select EmailAddress, LastLogonDate, SamAccountName
CraCra
  • 1
  • 2