Multiple where clause inside variable

Welcome Forums General PowerShell Q&A Multiple where clause inside variable

This topic contains 9 replies, has 5 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #133934

    Participant
    Topics: 2
    Replies: 5
    Points: 23
    Rank: Member

    Can anyone help me with the syntax here, if it's possible?
    Basically, I'm trying to create a variable with some filters to add to a where clause.

    I would like to have a configuration variable that is used later in the code.

    For example:

    $ProfileFilter="*_test","*local"
    
    Get-UserProfiles | where-object {$_.username -notin $ProfileFilter}
    

    Not sure if I need to modify the syntax on the variable or use a different operator, etc.

    Thanks

  • #133961

    Participant
    Topics: 2
    Replies: 397
    Points: 689
    Helping Hand
    Rank: Major Contributor

    Hmmm... The -like operator is designed to work with only one wildcard filter at a time; it won't handle multiple.

    For cases like this, I'd recommend the -match operator (or -notmatch in this case). It uses regex, but once you get past that little hurdle it's very neat for relatively straightforward asks like this. | in regex is "or", so we'll make use of that, and note also that regex is a partial match by default, so we need less wildcards if that's what you want here (string ending in either of those terms).

    $ProfileFilter = "(_test|local)$"
    
    Get-UserProfiles | where-object {$_.username -notmatch $ProfileFilter}

    That match string essentially says "we want either of these two things in the parentheses, and immediately after that we should have the end of the string". $ is the "end of string" anchor in regex.

  • #134007

    Participant
    Topics: 2
    Replies: 5
    Points: 23
    Rank: Member

    The purpose of the variable is to make it easy for someone else to configure how the script will work.  They need to be able to enter plain text, not a regex.

    Thanks for the idea though.

    I could put the entire where clause in a variable, including the -or operators.  I really want to make it simpler than that for the person running the script though.

  • #134036

    Participant
    Topics: 12
    Replies: 215
    Points: 380
    Helping Hand
    Rank: Contributor

    Not exactly sure what you're after or what the issue is.
    But to me it sounds like you should parameterize the script.
    So that the script/function doesn't need to be modified in code for the scenarios you want to cover.

  • #134078

    Participant
    Topics: 6
    Replies: 55
    Points: 343
    Helping Hand
    Rank: Contributor

    You can also combine the solution of Fredrik and Joel

    param(
        [string[]]$Filter
    )
    
    $FilterRegex = $Filter -join '|'
    
    Get-UserProfiles | where-object {$_.username -notmatch $FilterRegex}
    

    Script usage

    MyScript.ps1 -Filter '_test','local'
  • #134108

    Participant
    Topics: 2
    Replies: 5
    Points: 23
    Rank: Member

    This is a huge script with a GUI.  It is being run like it is an application.  It already has a separate configuration file with about 30 variables that are user configurable.

  • #134111

    Moderator
    Topics: 8
    Replies: 739
    Points: 2,071
    Helping Hand
    Rank: Community Hero

    can you show us how you want the script to be called with an example ?

  • #134127

    Participant
    Topics: 2
    Replies: 5
    Points: 23
    Rank: Member

    I right-click the script and run with Powershell.  Then it opens the gui.

  • #134130

    Moderator
    Topics: 8
    Replies: 739
    Points: 2,071
    Helping Hand
    Rank: Community Hero

    You could explain more, What the UI will ask for ? Are you giving any inputs via textbox ? if so what all you will be giving as input. If possible please share the screenshot with your expected inputs.

  • #134135

    Participant
    Topics: 2
    Replies: 5
    Points: 23
    Rank: Member

    I clicking a button that calls a function.  The function lists all the user profiles on the computer.  Then it populates a gridview with the list.   I want to filter out profiles that match a certain criteria.   I do not want profiles to be listed if they end with a certain substring.

The topic ‘Multiple where clause inside variable’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort