Powershell exclusion

Sara 441 Reputation points
2023-06-05T16:56:01.0166667+00:00

I have a small script where I am trying to exclude names starting with "test", "dev" etc...

I want to create a separate variable as $exclusionpattern = @("dev" , "test") -join "|"

and want to use that in foreach loop to exclude names containing dev or test, also with underscores, can someone help me with their thoughts

    foreach ($group in $Data) {
        $testName = $group.Name.Split(".")[4]
        $Componentname = $group.name.split(".")[5]
        $Name          = $group.Name.Split(".")[6]
        
        If (($group.Group[-1].Value -eq "0.0") -and ($group.Group[-2].Value -eq "0.0")) {
            Write-Verbose "$testName $Componentname $Name is DOWN" -Verbose
        }
        ElseIf (($group.Group[-1].Value -eq "2.0") -and ($group.Group[-2].Value -eq "1.0")) {
            Write-Verbose "$testName $Componentname $Name is UP" -Verbose
        }
        ElseIf (($group.Group[-1].Value -eq "1.0") -and ($group.Group[-2].Value -eq "2.0")) {
            Write-Verbose "$testName $Componentname $Name is OUT OF SERVICE" -Verbose
        }
    }

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,575 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,664 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,386 Reputation points
    2023-06-05T18:45:20.09+00:00

    Something like this?

    $Names = "ThisIsATest", "ThisTestIsOkay","IContainA_Character","ImNotARobot"
    $exclusionpattern = @("dev" , "test", "_") -join "|"
    
    Foreach ($name in $Names){
        if ($name -match $exclusionpattern){
            "Dropping $name"
        }
        else{
            "Name is Okay $name"
        }
    }
    

  2. Rich Matheisen 47,386 Reputation points
    2023-06-06T14:41:14.7533333+00:00

    You have three variables in your example that have "name" as part of their name. You can replace the "$name" in the conditional expression with another one if I chose the wrong one.

    $exclusionpattern = @("dev", "test", "_") -join "|"
    
    foreach ($group in $Data) {
        $testName = $group.Name.Split(".")[4]
        $Componentname = $group.name.split(".")[5]
        $Name          = $group.Name.Split(".")[6]
        
        if ($name -match $exclusionpattern){
            continue
        }
    
        If (($group.Group[-1].Value -eq "0.0") -and ($group.Group[-2].Value -eq "0.0")) {
            Write-Verbose "$testName $Componentname $Name is DOWN" -Verbose
        }
        ElseIf (($group.Group[-1].Value -eq "2.0") -and ($group.Group[-2].Value -eq "1.0")) {
            Write-Verbose "$testName $Componentname $Name is UP" -Verbose
        }
        ElseIf (($group.Group[-1].Value -eq "1.0") -and ($group.Group[-2].Value -eq "2.0")) {
            Write-Verbose "$testName $Componentname $Name is OUT OF SERVICE" -Verbose
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.