Functions/Set-IAViewFilter.ps1

Function Set-IAViewFilter {
    param(
        [Parameter(Mandatory = $true)]
        [PSObject] $View,
        [Parameter(Mandatory = $true)]
        [Guid[]] $AzureADObjectIds
    )
    #remove current view filters for this view
    $currentViewFilters = (Invoke-IAQuery -QueryUrl "ViewFilters?`$filter=ViewId eq $($View.Id)" -Method Get).value
    $currentObjectIds = $currentViewFilters.objectId
    $viewFilters = [System.Collections.ArrayList]@()

    #generate viewfilter objects
    foreach ($objectId in $AzureADObjectIds) {
        $viewFilter = $null
        $viewFilter = [pscustomobject]@{
            Id = [System.Guid]::NewGuid()
            ViewId   = $View.Id
            ObjectId = $objectId
        }
        $null = $viewFilters.Add($viewFilter)
        
        $Uri = "ViewFilters"
        $Body = $viewFilter | ConvertTo-JSON
        #Add filter is it does not currently exist
        if($objectId -notin $currentObjectIds){
            $Body = $viewFilter | ConvertTo-JSON
            Write-Verbose "Creating $Uri`nBody:`n$Body"
            $response = Invoke-IAQuery -QueryUrl $Uri -Method Post -Body $Body
            write-verbose $response
        }
        else {
            $Body = $viewFilter | Select-Object ViewId,ObjectId | ConvertTo-JSON
            Write-Verbose "Already Exists $Uri`nBody:`n$Body"
        }
    }

    #Cleanup extras
    $viewFiltersToDelete = $currentViewFilters | Where-Object { $_.ObjectId -notin $viewFilters.ObjectId }
    #generate viewfilter objects
    foreach ($viewFilterDelete in $viewFiltersToDelete) {
        $uri = "ViewFilters($($viewFilterDelete.Id))"
        write-verbose "Deleting: $uri"
        $response = Invoke-IAQuery -QueryUrl $uri -Method Delete 
        write-verbose $response
    }
}