Reputation: 23
I'm trying to resolve permissions on shared mailboxes
into their user objects. These are listed as on the mailbox by their O365 SAMAccountName
. I have these as an array of strings and I'm searching for the user object like so:
foreach ($samaccountname in $SANRWUsers)
{
Write-Host "Looking for" $samaccountname
$user = Get-User -Filter "SamAccountName -eq $($samaccountname)"
# Do other stuff...
}
Mostly this is working out. Most of the SAMAccountNames are of the form "abcd1111111-222222222
".
However, some look like this:
$A75T20-QSRL3G5B6T0L
That's a string, not a variable name. What I get when the above code block searches for that is this:
Cannot bind parameter 'Filter' to the target. Exception setting "Filter": ""-QSRL3G5B6T0L" is not a valid operator. For a list of supported operators see the command help.
"SamAccountName -eq $A75T20-QSRL3G5B6T0L" at position 27."
I'm very far from a Powershell expert from what I can figure out it's taking the dollar sign on the front of the string to be a sigil and trying to treat it as a variable. How can I get Powershell to not do that?
It seems as though the accounts that have this form of SAMAccountName are fairly old ones and were created in the Live@Edu days, early 2011 or so. There aren't many compared to the many tens of thousands created since but there's enough for this to be an issue.
Thanks in advance.
Upvotes: 1
Views: 2063
Reputation: 200503
You need to quote the nested string in your filter expression:
$user = Get-User -Filter "SamAccountName -eq '$samaccountname'"
If you provide a literal string instead of a variable in the filter expression you need to escape the $
to ensure that PowerShell doesn't treat the string as a variable:
$user = Get-User -Filter "SamAccountName -eq '`$A75T20-QSRL3G5B6T0L'"
Upvotes: 2