I’m writing a function which contains a [String]Filter parameter. I’d like the value of the parameter to be declared the same way the filter is declared in Get-ADUser -Filter. Wrapping the parameter value in double quotation marks won’t work, as it interferes with the double quotation marks present in the filter. What do I need to do so that the following code will work?
. .\MyAdvancedFunction.ps1
MyAdvancedFunction -Filter { Name -Like “Test” }
$Filter
{ Name -Like “Test” }
Get-ADUser -Filter $Filter
PowerShell will happily convert a ScriptBlock object to a String for you. That’s what happens when you call the AD cmdlets and use the syntax of -Filter { Something }.
As Dave mentioned the variable only exists within the function. If you want to verify the contents of the $Filter variable while you are developing you can add it into the function. Alternatively, you can use the debug functionality in ISE to check it.
function test-function {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false)]
[String]$Filter
)
"This is the contents of the Filter variable: $Filter"
Get-ADUser -Filter $Filter
}
test-function -Filter { Name -like "test*" }