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:\>$SamAccountNa开发者_如何学编程me = "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!
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()
As seen in the comments, add single quotes around the variables, or your filter result has incorrect syntax.
Get-User -Filter "SamAccountName -eq '$SamAccountName'"
When passing parameters directly you can just pass the variable. But in this case you are building a properly formatted query string, and the single quotes are part of that.
When you get a full answer, don't leave it as a comment... create it as a full answer.
精彩评论