obious
obious

Reputation: 629

Powershell: Passing variable into scriptblock within function

So take this code:

function populate([string] $group)
{
    $listbox.items.clear()
    Get-ADGroup -filter {cn -like "*$group*"} | 
        foreach {$listbox.items.add($_.DistinguishedName)}
}

$go.Add_Click(
{
    populate -group $text.text 
})

I'm trying to take the variable $group and pass it to {cn -like "*$group*"} but I can't get it to work. Does anybody have any ideas?

Thanks

Upvotes: 1

Views: 1154

Answers (1)

Keith Hill
Keith Hill

Reputation: 201652

Try this:

$pattern = "*$group*"
Get-ADGroup -filter {cn -like $pattern} ...

Upvotes: 1

Related Questions