0

I try to understand why this does not work

get-aduser -filter {Name -like "*$($Name.text)*"}

I understand that $($name.text) means can you take this this not a string value "" .

Thank you for your help .

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • You getting an error? What happens why you run that snippet on your system? – Adam Dec 14 '18 at 20:47
  • 1
    the reason it fails is because - despite LOTS of online samples of scriptblocks - the `-Filter` parameter is actually a **_string_** parameter. [*sigh ...*] the scriptblock will work for very simple things, but compound/complex $Vars will almost always fail. ///// the solution is to either save the complex $Var to a simpler one and use the simpler one, OR use a proper filter string. the following otta work ... `-Filter "Name -like '*$($Name.Text)'"`. note the outer double quotes and the inner singletons. ///// i cannot test that [no AD access], but it matches other samples. – Lee_Dailey Dec 14 '18 at 21:16
  • 1
    To provide background information on @Lee_Dailey's helpful comment: It's best to [avoid the use of script blocks (`{ ... }`) as `-Filter` arguments](https://stackoverflow.com/a/44184818/45375). – mklement0 Dec 14 '18 at 21:19

1 Answers1

0

I'm not 100% sure why that doesn't work, but you could make a temporary string to get around this.

$tmpStr = "*" + $Name.text + "*"
Get-ADUser -Filter {Name -like $tmpStr}
  • This is a viable workaround, but it's best to [avoid the use of script blocks (`{ ... }`) as `-Filter` arguments](https://stackoverflow.com/a/44184818/45375) altogether. – mklement0 Dec 14 '18 at 21:18