# Add ExcludeVMTagIds
$AllProtectionSourceObject = Get-CohesityProtectionSourceObject
$AllProtectionGroups = Get-CohesityProtectionJob -Names "ZM-TEST" | Sort-Object Name

ForEach ($ProtectionGroup in $AllProtectionGroups) {
    Write-Warning ("Start " + $ProtectionGroup.name)
    # Tag to Add to Exclude
    $SourceObjectsToAddToExclude = $AllProtectionSourceObject | where-object { $_.Name -eq "Test" -and $_.ParentId -eq $ProtectionGroup.parentSourceId }

    If ($ProtectionGroup.excludeVmTagIds) {
        # if an exclusion already exists
        $ProtectionGroup.excludeVmTagIds += , @($SourceObjectsToAddToExclude.Id)
        Set-CohesityProtectionJob -ProtectionJob $ProtectionGroup -confirm:$false
    }    Else {
        # if an exclusion does not already exist
        $ProtectionGroup | Add-Member -MemberType NoteProperty -Name excludeVmTagIds -Value @(, @($SourceObjectsToAddToExclude.Id))
        Set-CohesityProtectionJob -ProtectionJob $ProtectionGroup -confirm:$false
    }
}







# Remove ExcludeVMTagIds
$AllProtectionSourceObject = Get-CohesityProtectionSourceObject
$AllProtectionGroups = Get-CohesityProtectionJob -Names "ZM-TEST" | Sort-Object Name

ForEach ($ProtectionGroup in $AllProtectionGroups) {
    Write-Warning ("Start " + $ProtectionGroup.name)
    # Tag to Remove to Exclude
    $SourceObjectsToRemoveFromExclude = $AllProtectionSourceObject | Where-Object { $_.Name -eq "Test" -and $_.ParentId -eq $ProtectionGroup.parentSourceId }

    if (! $ProtectionGroup.PSObject.Properties['excludeVmTagIds']) {

    }
    else {
        $NewArray = @()
        ForEach ($item in $ProtectionGroup.PSObject.Properties['excludeVmTagIds'].value) {
            if ($item -ne $SourceObjectsToRemoveFromExclude.id) { $NewArray += $item }
        }
        
        If(@($NewArray).count -gt 0){
            $ProtectionGroup.excludeVmTagIds = ,@($NewArray)
            Set-CohesityProtectionJob -ProtectionJob $ProtectionGroup -confirm:$false
        }
        If(@($NewArray).count -eq 0){
            $ProtectionGroup.PSObject.Properties.remove('excludeVmTagIds')
            Set-CohesityProtectionJob -ProtectionJob $ProtectionGroup -confirm:$false
        }
    }
}



# Add VMTagIds
$AllProtectionSourceObject = Get-CohesityProtectionSourceObject
$AllProtectionGroups = Get-CohesityProtectionJob -Names "ZM-TEST" | Sort-Object Name

ForEach ($ProtectionGroup in $AllProtectionGroups) {
    Write-Warning ("Start " + $ProtectionGroup.name)
    # Tag to Add to Include
    $SourceObjectsToAddToInclude = $AllProtectionSourceObject | where-object { $_.Name -eq "ITD-POC-Video" -and $_.ParentId -eq $ProtectionGroup.parentSourceId }

    If ($ProtectionGroup.VmTagIds) {
        # if an exclusion already exists
        $ProtectionGroup.VmTagIds += , @($SourceObjectsToAddToInclude.Id)
        Set-CohesityProtectionJob -ProtectionJob $ProtectionGroup -confirm:$false
    }    Else {
        # if an exclusion does not already exist
        $ProtectionGroup | Add-Member -MemberType NoteProperty -Name VmTagIds -Value @(, @($SourceObjectsToAddToInclude.Id))
        Set-CohesityProtectionJob -ProtectionJob $ProtectionGroup -confirm:$false
    }
}



# Remove VMTagIds
$AllProtectionSourceObject = Get-CohesityProtectionSourceObject
$AllProtectionGroups = Get-CohesityProtectionJob -Names "ZM-TEST" | Sort-Object Name

ForEach ($ProtectionGroup in $AllProtectionGroups) {
    Write-Warning ("Start " + $ProtectionGroup.name)
    # Tag to Remove from Include
    $SourceObjectsToRemoveFromInclude = $AllProtectionSourceObject | where-object { $_.Name -eq "ITD-POC-Video" -and $_.ParentId -eq $ProtectionGroup.parentSourceId }

    If ($ProtectionGroup.VmTagIds) {
        # if an exclusion already exists
        $ProtectionGroup.VmTagIds += , @($SourceObjectsToRemoveFromInclude.Id)
        Set-CohesityProtectionJob -ProtectionJob $ProtectionGroup -confirm:$false
    }    Else {
        # if an exclusion does not already exist
        $ProtectionGroup | Add-Member -MemberType NoteProperty -Name VmTagIds -Value @(, @($SourceObjectsToRemoveFromInclude.Id))
        Set-CohesityProtectionJob -ProtectionJob $ProtectionGroup -confirm:$false
    }
}