Cannot use variable with Get-User -Filter in Exchange Management Console

I cannot use the variable in the situation below.

[PS] C:\>Get-User -Filter {SamAccountName -eq "Test.Smith"}
Name                                                        RecipientType
----                                                        -------------
Test Smith                                                  UserMailbox

[PS] C:\>$SamAccountName = "Test.Smith"
[PS] C:\>Get-User -Filter {SamAccountName -eq $SamAccountName}
[PS] C:\>echo $SamAccountName
Test.Smith
[PS] C:\>

You can see that the command works fine when entering a name, but not when using a variable. Thank!

+3
source share
2 answers

I do not have access to this cmdlet, are you sure that it accepts a script block, not a string? If a string is required, try the following:

Get-User -Filter "SamAccountName -eq $SamAccountName"

If a script block attempt is actually required:

Get-User -Filter {SamAccountName -eq $SamAccountName}.GetNewClosure()
+4
source

As you can see from the comments, add single quotes around the variables, or your filter result has the wrong syntax.

 Get-User -Filter "SamAccountName -eq '$SamAccountName'"

. , .

, ... .

+1

Source: https://habr.com/ru/post/1771352/


All Articles