Author Posts

December 16, 2016 at 6:41 am

I wonder if it is legal/valid to expand a local variable inside a script block, which will be used on some cmdlet PARAMETER ( maybe AD cmdlet ):

$sb={Name -like "$a*"} # the expansion would occur here -> $sb={Name -like 'Ro*'}
Get-ADUser -Filter $sb ...
# would use $sb again with other cmdlet parameters

Is it valid/legal?

December 16, 2016 at 2:24 pm

My lawyer says don't do that. He wouldn't defend the use of like.

$a='potter, dan'
$sb={anr -eq $a}
Get-ADUser -Filter $sb

December 16, 2016 at 5:27 pm

Definitely, a good lawyer, so good that "escaped" my question and answered you about a question I did not do. What about my original question: " expanding a string inside a script block: is it valid/legal (under PS rules)/correct?
In other words, is this assignment

$sb={Name -like "$a*"}

equivalent to this one

$sb={Name -like " Ro*"}

under the context of my original question?

December 16, 2016 at 5:32 pm

I have yet to find the rule book for powershell. Does it work?

December 16, 2016 at 8:27 pm

Please, in this thread just answer my question(s), if you know the answer ( it doesn't seem to be true ).
As to your question, please, start your own thread, and wait some reply...

December 17, 2016 at 12:00 pm

Is it valid/legal?


December 20, 2016 at 11:46 am

The good thing about the Active Directory cmdlets is that you can provide a normal string for the filter with the same syntax.

$a = 'Ro'
$filter = " Name -like '$a*' "
Get-ADUser -Filter $filter

I hope that helps.

December 20, 2016 at 11:50 am

As alternative you could also you below syntax if you really want a scriptblock:

$a = 'Ro'
$sb = [scriptblock]::Create("Name -like '$a*'")
Get-ADUser -Filter $sb

December 20, 2016 at 6:53 pm

That's it! Thanks.