@@ -445,9 +445,12 @@ function Get-TargetResource
445445 $policyApps = Get-MgBetaDeviceAppManagementAndroidManagedAppProtectionApp - AndroidManagedAppProtectionId $Id
446446
447447 $appsArray = @ ()
448- foreach ($app in $policyApps )
448+ if ($policy .AppGroupType -eq ' selectedPublicApps ' )
449449 {
450- $appsArray += $app.MobileAppIdentifier.AdditionalProperties.packageId
450+ foreach ($app in $policyApps )
451+ {
452+ $appsArray += $app.MobileAppIdentifier.AdditionalProperties.packageId
453+ }
451454 }
452455
453456 $assignmentsValues = Get-MgBetaDeviceAppManagementAndroidManagedAppProtectionAssignment - AndroidManagedAppProtectionId $policy.Id
@@ -1017,7 +1020,7 @@ function Set-TargetResource
10171020 $BoundParameters.CustomBrowserDisplayName = $ManagedBrowserValuesHash.CustomBrowserDisplayName
10181021 $BoundParameters.CustomBrowserPackageId = $ManagedBrowserValuesHash.CustomBrowserPackageId
10191022
1020- if (( $Ensure -eq ' Present' ) -and ( $currentPolicy.Ensure -eq ' Absent' ) )
1023+ if ($Ensure -eq ' Present' -and $currentPolicy.Ensure -eq ' Absent' )
10211024 {
10221025 $createParameters = ([Hashtable ]$BoundParameters ).Clone()
10231026 $createParameters.Remove (' Id' ) | Out-Null
@@ -1029,9 +1032,9 @@ function Set-TargetResource
10291032
10301033 if ($newPolicy.Id )
10311034 {
1032- Write-Verbose - Message " Update targetApps for Android App Protection Policy with Id {$ ( $newpolicy .Id ) } and DisplayName {$DisplayName }"
1033- $targetApps = Get-IntuneAppProtectionPolicyAndroidAppsToHashtable - Apps $Apps
1034- $Url = (Get-MSCloudLoginConnectionProfile - Workload MicrosoftGraph).ResourceUrl + " beta/deviceAppManagement/androidManagedAppProtections('$ ( $policy .Id ) ')/targetApps"
1035+ Write-Verbose - Message " Update targetApps for Android App Protection Policy with Id {$ ( $newPolicy .Id ) } and DisplayName {$DisplayName }"
1036+ $targetApps = Get-IntuneAppProtectionPolicyAndroidAppsToHashtable - Apps $Apps - AppGroupType $AppGroupType
1037+ $Url = (Get-MSCloudLoginConnectionProfile - Workload MicrosoftGraph).ResourceUrl + " beta/deviceAppManagement/androidManagedAppProtections('$ ( $newPolicy .Id ) ')/targetApps"
10351038 Invoke-MgGraphRequest - Method POST - Uri $Url - Body $targetApps
10361039
10371040 $assignmentsHash = ConvertTo-IntunePolicyAssignment - IncludeDeviceFilter:$true - Assignments $Assignments
@@ -1041,7 +1044,7 @@ function Set-TargetResource
10411044 - Repository ' deviceAppManagement/androidManagedAppProtections'
10421045 }
10431046 }
1044- elseif (( $Ensure -eq ' Present' ) -and ( $currentPolicy.Ensure -eq ' Present' ) )
1047+ elseif ($Ensure -eq ' Present' -and $currentPolicy.Ensure -eq ' Present' )
10451048 {
10461049 $updateParameters = ([Hashtable ]$BoundParameters ).Clone()
10471050 $updateParameters.Remove (' Id' ) | Out-Null
@@ -1052,7 +1055,7 @@ function Set-TargetResource
10521055 Update-MgBetaDeviceAppManagementAndroidManagedAppProtection - AndroidManagedAppProtectionId $currentPolicy.Id - BodyParameter $updateParameters
10531056
10541057 Write-Verbose - Message " Update targetApps for Android App Protection Policy with Id {$ ( $currentPolicy.Id ) } and DisplayName {$DisplayName }"
1055- $targetApps = Get-IntuneAppProtectionPolicyAndroidAppsToHashtable - Apps $Apps
1058+ $targetApps = Get-IntuneAppProtectionPolicyAndroidAppsToHashtable - Apps $Apps - AppGroupType $AppGroupType
10561059 $Url = (Get-MSCloudLoginConnectionProfile - Workload MicrosoftGraph).ResourceUrl + " beta/deviceAppManagement/androidManagedAppProtections('$ ( $currentPolicy.Id ) ')/targetApps"
10571060 Invoke-MgGraphRequest - Method POST - Uri $Url - Body $targetApps
10581061
@@ -1457,8 +1460,17 @@ function Test-TargetResource
14571460 Add-M365DSCTelemetryEvent - Data $data
14581461 # endregion
14591462
1463+ $postProcessingScript = {
1464+ param ($DesiredValues , $CurrentValues , $ValuesToCheck , $ignore )
1465+ if ($DesiredValues.AppGroupType -ne ' SelectedPublicApps' )
1466+ {
1467+ $ValuesToCheck.Remove (' Apps' )
1468+ }
1469+ return [System.Tuple [Hashtable , Hashtable , Hashtable ]]::new($DesiredValues , $CurrentValues , $ValuesToCheck )
1470+ }
14601471 $result = Test-M365DSCTargetResource - DesiredValues $PSBoundParameters `
1461- - ResourceName $ ($MyInvocation.MyCommand.Source ).Replace(' MSFT_' , ' ' )
1472+ - ResourceName $ ($MyInvocation.MyCommand.Source ).Replace(' MSFT_' , ' ' ) `
1473+ - PostProcessing $postProcessingScript
14621474 return $result
14631475}
14641476
@@ -1611,26 +1623,67 @@ function Get-IntuneAppProtectionPolicyAndroidAppsToHashtable
16111623{
16121624 [CmdletBinding ()]
16131625 [OutputType ([System.Collections.Hashtable ])]
1614- param (
1626+ param
1627+ (
16151628 [Parameter (Mandatory = $true )]
1629+ [AllowEmptyCollection ()]
16161630 [System.String []]
1617- $Apps
1631+ $Apps ,
1632+
1633+ [Parameter (Mandatory = $true )]
1634+ [ValidateSet (' selectedPublicApps' , ' allCoreMicrosoftApps' , ' allMicrosoftApps' , ' allApps' )]
1635+ [System.String ]
1636+ $AppGroupType
16181637 )
16191638
16201639 $formattedApps = @ ()
1640+ $allApps = (Get-MgBetaDeviceAppManagementManagedAppStatus - ManagedAppStatusId managedAppList).AdditionalProperties.content.appList | Where-Object {
1641+ $_.appIdentifier .' @odata.type' -eq ' #microsoft.graph.androidMobileAppIdentifier'
1642+ }
1643+
1644+ switch ($AppGroupType )
1645+ {
1646+ ' selectedPublicApps'
1647+ {
1648+ if ($Apps.Count -eq 0 )
1649+ {
1650+ throw " AppGroupType is set to 'selectedPublicApps' but no Apps were provided."
1651+ }
1652+ }
1653+ ' allCoreMicrosoftApps'
1654+ {
1655+ $Apps = $allApps | Where-Object appGroups -EQ ' coreMicrosoft' | ForEach-Object {
1656+ $_.appIdentifier.bundleId
1657+ }
1658+ }
1659+ ' allMicrosoftApps'
1660+ {
1661+ $Apps = $allApps | Where-Object appGroups -EQ ' microsoft' | ForEach-Object {
1662+ $_.appIdentifier.bundleId
1663+ }
1664+ }
1665+ ' allApps'
1666+ {
1667+ $Apps = $allApps | ForEach-Object {
1668+ $_.appIdentifier.bundleId
1669+ }
1670+ }
1671+ }
1672+
16211673 foreach ($app in $Apps )
16221674 {
16231675 $formattedApps += @ {
16241676 id = $app + ' .android'
16251677 mobileAppIdentifier = @ {
1626- ' @odata.type' = ' #microsoft.graph.AndroidMobileAppIdentifier '
1678+ ' @odata.type' = ' #microsoft.graph.androidMobileAppIdentifier '
16271679 packageId = $app
16281680 }
16291681 }
16301682 }
16311683
16321684 return @ {
16331685 apps = $formattedApps
1686+ appGroupType = $AppGroupType
16341687 }
16351688}
16361689
0 commit comments