locked
Can I put an operator in a variable? RRS feed

  • Question

  • Hi,

    Can I put an operator in a variable?

    F.e..

    if ($Exact) $ {$Operator = '-eq'} else {$operator = 'like'}

    => get-aduser -properties * -filter {surname $operator 'Doe'}

    It does not work (gives "Error parsing query:'), I wonder if it can be done and yes how?

    J.


    Jan Hoedt

    Thursday, February 16, 2017 5:25 PM

Answers

  • Yes, if you dynamically generate the scriptblock needed by the -Filter parameter. Example:


    $operator = '-like'
    $surname = 'johnson'
    
    $scriptBlock = [scriptblock]::Create("surname $operator '$surname'")
    
    Get-ADUser -Filter $scriptBlock
    



    -- Bill Stewart [Bill_Stewart]


    • Edited by Bill_Stewart Thursday, February 16, 2017 9:25 PM
    • Proposed as answer by Richard MuellerMVP Friday, February 17, 2017 3:13 AM
    • Marked as answer by janhoedt Tuesday, February 21, 2017 10:01 AM
    Thursday, February 16, 2017 5:30 PM