views:

29

answers:

1

I cannot seem to use 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 the command works fine when I type out the name, but not when I use a variable. Thanks!

A: 

I don't have access to this cmdlet, are you sure it takes a scriptblock and not a string? If it takes a string try this:

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

If it really takes a scriptblock try:

Get-User -Filter {SamAccountName -eq $SamAccountName}.GetNewClosure()
Keith Hill
Thanks Keith. I am not sure which it takes, but the quote method generated an error, and the {}.GetNewClosure() made no difference. I copied this line from Step 5 here - http://technet.microsoft.com/en-us/library/bb936719%28EXCHG.80%29.aspx#NewUsrps1
RRR
The docs say it is supposed to take a string - go figure. http://technet.microsoft.com/en-us/library/aa996896(EXCHG.80).aspx
Keith Hill
Example two from those docs: `Get-User -Filter "Title -like '*Manager'"`. Try `"SamAccountName -eq '$SamAccountName'"`.
Keith Hill
Ah! That's it! I needed the single quotes. The correct command is get-user -filter "SamAccountName -eq '$samAccountName'" Thanks for your help!
RRR