0

I am trying to load a variable into get-aduser -filter $variable.Property but I can't seem to get it to work.

Here is the code I am working on. This is only one piece of what I am trying to do.

$DisabledMailboxes = get-mailbox | ?{$_.RecipientTypeDetails -eq 'usermailbox' -and $_.AccountDisabled -eq $true} | select UserPrincipalName,PrimarySmtpAddress,RecipientTypeDetails

Foreach ($mb in $DisabledMailboxes) {

   $users = Get-aduser -Filter "EmailAddress -eq '$mb.PrimarySmtpAddress'" | select Name,SamAccountName,UserPrincipalName

}

here is what I am trying to accomplish

  1. Insert all disabled mailboxes in O365 into variable.
  2. Find the Local AD user account based on the email address from disabledmailboxes variable.
  3. find out if user is a member of a specific group.
  4. finally if user is a member of specific group remove from said group and add them to a different group.

The get-aduser -filter yada yada.. is only used so I can get the SamAccountName and then in turn use get-AdPrincipalGroupMembership.

Any help would be greatly appreciated.

Thanks-

Theo
  • 57,719
  • 8
  • 24
  • 41
TDx2
  • 35
  • 3
  • 1
    Probably need $($var.prop) as usual inside double quotes. – js2010 Mar 08 '23 at 19:18
  • Yep that was what it was. Thanks. I ended up using Get-aduser -Filter "EmailAddress -eq '$($mb.PrimarySmtpAddress)'" | select Name,SamAccountName,UserPrincipalName – TDx2 Mar 08 '23 at 20:02

0 Answers0