0

I'm trying to see if a value exists in an array. The index of function works when I manually put in a string. However when I use the value's of a foreach loop it doesn't work, what am I doing wrong? (see picture for more info)

                $groups = Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -eq "Global"  } -Properties isCriticalSystemObject | Where-Object { !($_.IsCriticalSystemObject) -and !($_.Name -eq "DnsUpdateProxy") }
                
            $currentFlexAssets = (Get-ITGlueFlexibleAssets -filter_flexible_asset_type_id $api__flex_asset_id -filter_organization_id $api__org_id )                
            
            
            # Delete groups from IT Glue that no longer exist in AD
            $api__flex_asset_id = ''
            Foreach ($asset in $currentFlexAssets.data.attributes.name) {
                        
                $asset
                
                $fa_index = [array]::indexof($groups.Name ,'$asset')
            
                $fa_index
                                    
                #if($fa_index -eq '-1') {
                #   Write-Host "Destroy = " $asset 
                #}
            }

powershell console

1
  • [array]::indexof($groups.Name, $asset) Commented Jul 27, 2020 at 11:44

1 Answer 1

1

As Mathias already commented, you should not use single quotes around the $asset variable. By using single quotes, the IndexOf is comparing against the literal value '$asset', not what this variable contains.

The IndexOf() method either gives you an int value of -1 if not found, or the actual array index of the string you're looking for. However, be aware that IndexOf is Case-Sensitive and because of that may not find what you want.

To make it compare case-insensitive, you can do this:

$groups = Get-ADGroup -Filter "GroupCategory -eq 'Security' -and GroupScope -eq 'Global'" -Properties isCriticalSystemObject | 
          Where-Object { !($_.IsCriticalSystemObject) -and !($_.Name -eq "DnsUpdateProxy") }

$currentFlexAssets = (Get-ITGlueFlexibleAssets -filter_flexible_asset_type_id $api__flex_asset_id -filter_organization_id $api__org_id )

# make all items in the array lowercase for the IndexOf() method
$groupsNames = $groups.Name | ForEach-Object { $_.ToLower() }

Then, inside your foreach ($asset in $currentFlexAssets.data.attributes.name) loop do:

$fa_index = [array]::IndexOf($groupsNames, $asset.ToLower())
if($fa_index -lt 0) {   # IndexOf() returns an int, so son't quote here
     Write-Host "Destroy = $asset"
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply, the quotes where still there due to testing. When I run the script setting all values to lower still $fa_index only returns -1 resulting in destroying all the assets. The value $asset comes from $currentFlexAssets.data.attributes.name and seems therefore not to be a string value which I can compare with IndexOf is that correct?
@SebastiaanCastenmiller Then I suggest to add a Write-Host to see what you get with $currentFlexAssets.data.attributes.name. I cannot test that myself.
this is what powershell returns on write-host $currentFlexAssets.data.attributes.name --------------------------------- GG_Sales GG_Marketing GG_Buitendienst GG_Inkoop Folder Redirection Users test -----------------------------
@SebastiaanCastenmiller Is that one string or an array? Are the lines with dashes part of the name attribute value?
there all on a separate line
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.