match a value in an array

Sebastiaan Castenmiller 1 Reputation point
2020-07-27T12:59:58.377+00:00

Hi,

I've got this script that tells me if a value exits in groups.name now I want it to tell me when it doesn't exist.

            ForEach($asset in $currentFlexAssets.data.attributes.name)
            {
                ForEach($group in $groups.Name)
                {
                    If ($asset -match $group)
                    {
                        Write-Host "Keep = " $asset 
                    }
                }
            }

Is there something like If ($asset -match $group.name) ??

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,455 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 32,736 Reputation points
    2020-07-27T13:28:24.29+00:00
            ForEach($asset in $currentFlexAssets.data.attributes.name)
                $Found = $false
                ForEach($group in $groups.Name)
                    If ($asset -match $group) {
                        Write-Host "Keep = " $asset
                        $Found = $true
                    }
                }
                if ($Found -eq $false) {
                    "Asset not found."
                }
            }
    
    0 comments No comments

  2. Rich Matheisen 45,831 Reputation points
    2020-07-27T19:26:26.437+00:00

    Why not this?

    ForEach($asset in $currentFlexAssets.data.attributes.name)
    {
        If ($groups.name -notcontains $asset){
            # do something
        }
    }
    
    0 comments No comments