DSCResources/MSFT_IntuneMobileAppsWebLink/MSFT_IntuneMobileAppsWebLink.psm1
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( #region resource generator code [Parameter(Mandatory = $true)] [System.String] $DisplayName, [Parameter()] [System.String] $Id, [Parameter(Mandatory = $true)] [ValidateSet('iosiPadOSWebClip', 'macOSWebClip', 'webApp', 'windowsWebApp')] [System.String] $TargetType, [Parameter()] [System.String] $AppUrl, [Parameter()] [System.Boolean] $FullScreenEnabled, [Parameter()] [System.Boolean] $PreComposedIconEnabled, [Parameter()] [System.Boolean] $IgnoreManifestScope, [Parameter()] [System.String] $TargetApplicationBundleIdentifier, [Parameter()] [System.Boolean] $UseManagedBrowser, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $Categories, [Parameter()] [System.String] $Description, [Parameter()] [System.String] $Developer, [Parameter()] [System.String] $InformationUrl, [Parameter()] [System.Boolean] $IsFeatured, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance] $LargeIcon, [Parameter()] [System.String] $Notes, [Parameter()] [System.String] $Owner, [Parameter()] [System.String] $PrivacyInformationUrl, [Parameter()] [System.String] $Publisher, [Parameter()] [System.String[]] $RoleScopeTagIds, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $Assignments, #endregion [Parameter()] [System.String] [ValidateSet('Absent', 'Present')] $Ensure = 'Present', [Parameter()] [System.Management.Automation.PSCredential] $Credential, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.Management.Automation.PSCredential] $ApplicationSecret, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [Switch] $ManagedIdentity, [Parameter()] [System.String[]] $AccessTokens ) Write-Verbose -Message "Getting configuration for the Intune Mobile Apps Web Link with Id {$Id} and DisplayName {$DisplayName}" foreach ($property in $Script:customProperties) { if ($PSBoundParameters.ContainsKey($property) -and $Script:odataToPropertiesMap.$TargetType -notcontains $property) { throw "Property '$property' is not supported for the target type '$TargetType'." } } try { if (-not $Script:exportedInstance -or $Script:exportedInstance.DisplayName -ne $DisplayName) { $ConnectionMode = New-M365DSCConnection -Workload 'MicrosoftGraph' ` -InboundParameters $PSBoundParameters #Ensure the proper dependencies are installed in the current environment. Confirm-M365DSCDependencies #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '') $CommandName = $MyInvocation.MyCommand $data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName ` -CommandName $CommandName ` -Parameters $PSBoundParameters Add-M365DSCTelemetryEvent -Data $data #endregion $nullResult = $PSBoundParameters $nullResult.Ensure = 'Absent' $getValue = $null #region resource generator code if (-not [System.String]::IsNullOrEmpty($Id)) { $getValue = Get-MgBetaDeviceAppManagementMobileApp -MobileAppId $Id -ExpandProperty 'categories' -ErrorAction SilentlyContinue } if ($null -eq $getValue) { Write-Verbose -Message "Could not find an Intune Mobile Apps Web Link with Id {$Id}" if (-not [System.String]::IsNullOrEmpty($DisplayName)) { $getValue = Get-MgBetaDeviceAppManagementMobileApp ` -Filter "DisplayName eq '$($DisplayName -replace "'", "''")'" ` -ExpandProperty 'categories' ` -ErrorAction SilentlyContinue | Where-Object ` -FilterScript { $_.AdditionalProperties.'@odata.type' -in @( "#microsoft.graph.iosiPadOSWebClip" "#microsoft.graph.macOSWebClip" "#microsoft.graph.windowsWebApp" "#microsoft.graph.webApp" ) } } } #endregion if ($null -eq $getValue) { Write-Verbose -Message "Could not find an Intune Mobile Apps Web Link with DisplayName {$DisplayName}." return $nullResult } } else { $getValue = Get-MgBetaDeviceAppManagementMobileApp -MobileAppId $Script:exportedInstance.Id ` -ExpandProperty 'categories' } $Id = $getValue.Id Write-Verbose -Message "An Intune Mobile Apps Web Link with Id {$Id} and DisplayName {$DisplayName} was found" #region resource generator code $complexCategories = @() foreach ($category in $getValue.Categories) { $myCategory = @{} $myCategory.Add('Id', $category.id) $myCategory.Add('DisplayName', $category.displayName) $complexCategories += $myCategory } $complexLargeIcon = $null if ($null -ne $getValue.LargeIcon.Value) { $complexLargeIcon = @{} $complexLargeIcon.Add('Type', $getValue.LargeIcon.Type) $complexLargeIcon.Add('Value', [System.Convert]::ToBase64String($getValue.LargeIcon.Value)) } #endregion $results = @{ #region resource generator code TargetType = $getValue.AdditionalProperties.'@odata.type'.Replace('#microsoft.graph.', '') AppUrl = $getValue.AdditionalProperties.appUrl UseManagedBrowser = $getValue.AdditionalProperties.useManagedBrowser FullScreenEnabled = $getValue.AdditionalProperties.fullScreenEnabled PreComposedIconEnabled = $getValue.AdditionalProperties.preComposedIconEnabled IgnoreManifestScope = $getValue.AdditionalProperties.ignoreManifestScope TargetApplicationBundleIdentifier = $getValue.AdditionalProperties.targetApplicationBundleIdentifier Categories = $complexCategories Description = $getValue.Description Developer = $getValue.Developer DisplayName = $getValue.DisplayName InformationUrl = $getValue.InformationUrl IsFeatured = $getValue.IsFeatured LargeIcon = $complexLargeIcon Notes = $getValue.Notes Owner = $getValue.Owner PrivacyInformationUrl = $getValue.PrivacyInformationUrl Publisher = $getValue.Publisher RoleScopeTagIds = $getValue.RoleScopeTagIds Id = $getValue.Id Ensure = 'Present' Credential = $Credential ApplicationId = $ApplicationId TenantId = $TenantId ApplicationSecret = $ApplicationSecret CertificateThumbprint = $CertificateThumbprint ManagedIdentity = $ManagedIdentity.IsPresent #endregion } $assignmentsValues = Get-MgBetaDeviceAppManagementMobileAppAssignment -MobileAppId $Id $assignmentResult = @() if ($assignmentsValues.Count -gt 0) { $assignmentResult += ConvertFrom-IntuneMobileAppAssignment -Assignments $assignmentsValues -IncludeDeviceFilter $true } $results.Add('Assignments', $assignmentResult) return [System.Collections.Hashtable] $results } catch { New-M365DSCLogEntry -Message 'Error retrieving data:' ` -Exception $_ ` -Source $($MyInvocation.MyCommand.Source) ` -TenantId $TenantId ` -Credential $Credential return $nullResult } } function Set-TargetResource { [CmdletBinding()] param ( #region resource generator code [Parameter(Mandatory = $true)] [System.String] $DisplayName, [Parameter()] [System.String] $Id, [Parameter(Mandatory = $true)] [ValidateSet('iosiPadOSWebClip', 'macOSWebClip', 'webApp', 'windowsWebApp')] [System.String] $TargetType, [Parameter()] [System.String] $AppUrl, [Parameter()] [System.Boolean] $FullScreenEnabled, [Parameter()] [System.Boolean] $PreComposedIconEnabled, [Parameter()] [System.Boolean] $IgnoreManifestScope, [Parameter()] [System.String] $TargetApplicationBundleIdentifier, [Parameter()] [System.Boolean] $UseManagedBrowser, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $Categories, [Parameter()] [System.String] $Description, [Parameter()] [System.String] $Developer, [Parameter()] [System.String] $InformationUrl, [Parameter()] [System.Boolean] $IsFeatured, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance] $LargeIcon, [Parameter()] [System.String] $Notes, [Parameter()] [System.String] $Owner, [Parameter()] [System.String] $PrivacyInformationUrl, [Parameter()] [System.String] $Publisher, [Parameter()] [System.String[]] $RoleScopeTagIds, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $Assignments, #endregion [Parameter()] [System.String] [ValidateSet('Absent', 'Present')] $Ensure = 'Present', [Parameter()] [System.Management.Automation.PSCredential] $Credential, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.Management.Automation.PSCredential] $ApplicationSecret, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [Switch] $ManagedIdentity, [Parameter()] [System.String[]] $AccessTokens ) Write-Verbose -Message "Setting configuration of the Intune Mobile Apps Web Link with Id {$Id} and DisplayName {$DisplayName}" #Ensure the proper dependencies are installed in the current environment. Confirm-M365DSCDependencies #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '') $CommandName = $MyInvocation.MyCommand $data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName ` -CommandName $CommandName ` -Parameters $PSBoundParameters Add-M365DSCTelemetryEvent -Data $data #endregion $currentInstance = Get-TargetResource @PSBoundParameters $BoundParameters = Remove-M365DSCAuthenticationParameter -BoundParameters $PSBoundParameters if ($BoundParameters.ContainsKey('LargeIcon')) { $complexLargeIcon = @{ type = $BoundParameters.LargeIcon.type value = [System.Convert]::FromBase64String($BoundParameters.LargeIcon.value) } $BoundParameters.Remove('LargeIcon') | Out-Null $BoundParameters.Add('LargeIcon', $complexLargeIcon) } foreach ($property in $Script:customProperties) { if ($BoundParameters.ContainsKey($property) -and $Script:odataToPropertiesMap.$TargetType -notcontains $property) { throw "Property '$property' is not supported for the target type '$TargetType'." } } $BoundParameters.Remove('AppUrl') | Out-Null $BoundParameters.Remove('Categories') | Out-Null if ($Ensure -eq 'Present' -and $currentInstance.Ensure -eq 'Absent') { Write-Verbose -Message "Creating an Intune Mobile Apps Web Link with DisplayName {$DisplayName}" $BoundParameters.Remove("Assignments") | Out-Null $createParameters = ([Hashtable]$BoundParameters).Clone() $createParameters = Rename-M365DSCCimInstanceParameter -Properties $createParameters $createParameters.Remove('Id') | Out-Null $keys = (([Hashtable]$createParameters).Clone()).Keys foreach ($key in $keys) { if ($null -ne $createParameters.$key -and $createParameters.$key.GetType().Name -like '*CimInstance*') { $createParameters.$key = Convert-M365DSCDRGComplexTypeToHashtable -ComplexObject $createParameters.$key } } #region resource generator code $createParameters.Add("@odata.type", "#microsoft.graph." + $BoundParameters.TargetType) $policy = New-MgBetaDeviceAppManagementMobileApp -BodyParameter $createParameters if ($PSBoundParameters.ContainsKey('Categories')) { Update-DeviceAppManagementAppCategory -App $policy -Categories $Categories } if ($policy.Id) { $assignmentsHash = ConvertTo-IntuneMobileAppAssignment -IncludeDeviceFilter:$true -Assignments $Assignments Update-DeviceAppManagementPolicyAssignment ` -AppManagementPolicyId $policy.Id ` -Assignments $assignmentsHash } #endregion } elseif ($Ensure -eq 'Present' -and $currentInstance.Ensure -eq 'Present') { Write-Verbose -Message "Updating the Intune Mobile Apps Web Link with Id {$($currentInstance.Id)}" $BoundParameters.Remove("Assignments") | Out-Null $updateParameters = ([Hashtable]$BoundParameters).Clone() $updateParameters = Rename-M365DSCCimInstanceParameter -Properties $updateParameters $updateParameters.Remove('Id') | Out-Null $keys = (([Hashtable]$updateParameters).Clone()).Keys foreach ($key in $keys) { if ($null -ne $pdateParameters.$key -and $updateParameters.$key.GetType().Name -like '*CimInstance*') { $updateParameters.$key = Convert-M365DSCDRGComplexTypeToHashtable -ComplexObject $updateParameters.MobileAppId } } #region resource generator code $UpdateParameters.Add("@odata.type", "#microsoft.graph." + $BoundParameters.TargetType) Update-MgBetaDeviceAppManagementMobileApp ` -MobileAppId $currentInstance.Id ` -BodyParameter $UpdateParameters if ($PSBoundParameters.ContainsKey('Categories')) { Update-DeviceAppManagementAppCategory -App $currentInstance -Categories $Categories -Compare } $assignmentsHash = ConvertTo-IntuneMobileAppAssignment -IncludeDeviceFilter:$true -Assignments $Assignments Update-DeviceAppManagementPolicyAssignment ` -AppManagementPolicyId $currentInstance.Id ` -Assignments $assignmentsHash #endregion } elseif ($Ensure -eq 'Absent' -and $currentInstance.Ensure -eq 'Present') { Write-Verbose -Message "Removing the Intune Mobile Apps Web Link with Id {$($currentInstance.Id)}" #region resource generator code Remove-MgBetaDeviceAppManagementMobileApp -MobileAppId $currentInstance.Id #endregion } } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( #region resource generator code [Parameter(Mandatory = $true)] [System.String] $DisplayName, [Parameter()] [System.String] $Id, [Parameter(Mandatory = $true)] [ValidateSet('iosiPadOSWebClip', 'macOSWebClip', 'webApp', 'windowsWebApp')] [System.String] $TargetType, [Parameter()] [System.String] $AppUrl, [Parameter()] [System.Boolean] $FullScreenEnabled, [Parameter()] [System.Boolean] $PreComposedIconEnabled, [Parameter()] [System.Boolean] $IgnoreManifestScope, [Parameter()] [System.String] $TargetApplicationBundleIdentifier, [Parameter()] [System.Boolean] $UseManagedBrowser, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $Categories, [Parameter()] [System.String] $Description, [Parameter()] [System.String] $Developer, [Parameter()] [System.String] $InformationUrl, [Parameter()] [System.Boolean] $IsFeatured, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance] $LargeIcon, [Parameter()] [System.String] $Notes, [Parameter()] [System.String] $Owner, [Parameter()] [System.String] $PrivacyInformationUrl, [Parameter()] [System.String] $Publisher, [Parameter()] [System.String[]] $RoleScopeTagIds, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $Assignments, #endregion [Parameter()] [System.String] [ValidateSet('Absent', 'Present')] $Ensure = 'Present', [Parameter()] [System.Management.Automation.PSCredential] $Credential, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.Management.Automation.PSCredential] $ApplicationSecret, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [Switch] $ManagedIdentity, [Parameter()] [System.String[]] $AccessTokens ) #Ensure the proper dependencies are installed in the current environment. Confirm-M365DSCDependencies #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '') $CommandName = $MyInvocation.MyCommand $data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName ` -CommandName $CommandName ` -Parameters $PSBoundParameters Add-M365DSCTelemetryEvent -Data $data #endregion Write-Verbose -Message "Testing configuration of the Intune Mobile Apps Web Link with Id {$Id} and DisplayName {$DisplayName}" $CurrentValues = Get-TargetResource @PSBoundParameters $ValuesToCheck = ([hashtable]$PSBoundParameters).Clone() $testResult = $true #Compare Cim instances foreach ($key in $PSBoundParameters.Keys) { $source = $PSBoundParameters.$key $target = $CurrentValues.$key if ($null -ne $source -and $source.GetType().Name -like '*CimInstance*') { $testResult = Compare-M365DSCComplexObject ` -Source ($source) ` -Target ($target) if (-not $testResult) { break } $ValuesToCheck.Remove($key) | Out-Null } } $ValuesToCheck.Remove('Id') | Out-Null $ValuesToCheck.Remove('AppUrl') | Out-Null $ValuesToCheck = Remove-M365DSCAuthenticationParameter -BoundParameters $ValuesToCheck Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)" Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $ValuesToCheck)" if ($testResult) { $testResult = Test-M365DSCParameterState -CurrentValues $CurrentValues ` -Source $($MyInvocation.MyCommand.Source) ` -DesiredValues $PSBoundParameters ` -ValuesToCheck $ValuesToCheck.Keys } Write-Verbose -Message "Test-TargetResource returned $testResult" return $testResult } function Export-TargetResource { [CmdletBinding()] [OutputType([System.String])] param ( [Parameter()] [System.String] $Filter, [Parameter()] [System.Management.Automation.PSCredential] $Credential, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.Management.Automation.PSCredential] $ApplicationSecret, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [Switch] $ManagedIdentity, [Parameter()] [System.String[]] $AccessTokens ) $ConnectionMode = New-M365DSCConnection -Workload 'MicrosoftGraph' ` -InboundParameters $PSBoundParameters #Ensure the proper dependencies are installed in the current environment. Confirm-M365DSCDependencies #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '') $CommandName = $MyInvocation.MyCommand $data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName ` -CommandName $CommandName ` -Parameters $PSBoundParameters Add-M365DSCTelemetryEvent -Data $data #endregion try { #region resource generator code [array]$getValue = Get-MgBetaDeviceAppManagementMobileApp ` -Filter $Filter ` -All ` -ExpandProperty 'categories' ` -ErrorAction Stop | Where-Object ` -FilterScript { $_.AdditionalProperties.'@odata.type' -in @( "#microsoft.graph.iosiPadOSWebClip" "#microsoft.graph.macOSWebClip" "#microsoft.graph.windowsWebApp" "#microsoft.graph.webApp" ) } #endregion $i = 1 $dscContent = '' if ($getValue.Length -eq 0) { Write-M365DSCHost -Message $Global:M365DSCEmojiGreenCheckMark -CommitWrite } else { Write-M365DSCHost -Message "`r`n" -DeferWrite } foreach ($config in $getValue) { $displayedKey = $config.Id if (-not [String]::IsNullOrEmpty($config.displayName)) { $displayedKey = $config.displayName } elseif (-not [string]::IsNullOrEmpty($config.name)) { $displayedKey = $config.name } Write-M365DSCHost -Message " |---[$i/$($getValue.Count)] $displayedKey" -DeferWrite $params = @{ Id = $config.Id DisplayName = $config.DisplayName TargetType = $config.AdditionalProperties.'@odata.type'.Replace('#microsoft.graph.', '') Ensure = 'Present' Credential = $Credential ApplicationId = $ApplicationId TenantId = $TenantId ApplicationSecret = $ApplicationSecret CertificateThumbprint = $CertificateThumbprint ManagedIdentity = $ManagedIdentity.IsPresent AccessTokens = $AccessTokens } $Script:exportedInstance = $config $Results = Get-TargetResource @Params if ($null -ne $Results.Categories) { $complexTypeStringResult = Get-M365DSCDRGComplexTypeToString ` -ComplexObject $Results.Categories ` -CIMInstanceName 'DeviceManagementMobileAppCategory' if (-not [System.String]::IsNullOrWhiteSpace($complexTypeStringResult)) { $Results.Categories = $complexTypeStringResult } else { $Results.Remove('Categories') | Out-Null } } if ($null -ne $Results.LargeIcon) { $complexTypeStringResult = Get-M365DSCDRGComplexTypeToString ` -ComplexObject $Results.LargeIcon ` -CIMInstanceName 'MicrosoftGraphmimeContent' if (-not [String]::IsNullOrWhiteSpace($complexTypeStringResult)) { $Results.LargeIcon = $complexTypeStringResult } else { $Results.Remove('LargeIcon') | Out-Null } } if ($Results.Assignments) { $complexTypeStringResult = Get-M365DSCDRGComplexTypeToString -ComplexObject $Results.Assignments -CIMInstanceName DeviceManagementMobileAppAssignment if ($complexTypeStringResult) { $Results.Assignments = $complexTypeStringResult } else { $Results.Remove('Assignments') | Out-Null } } $currentDSCBlock = Get-M365DSCExportContentForResource -ResourceName $ResourceName ` -ConnectionMode $ConnectionMode ` -ModulePath $PSScriptRoot ` -Results $Results ` -Credential $Credential ` -NoEscape @('Assignments', 'Categories', 'LargeIcon') $dscContent += $currentDSCBlock Save-M365DSCPartialExport -Content $currentDSCBlock ` -FileName $Global:PartialExportFileName $i++ Write-M365DSCHost -Message $Global:M365DSCEmojiGreenCheckMark -CommitWrite } return $dscContent } catch { Write-M365DSCHost -Message $Global:M365DSCEmojiRedX -CommitWrite New-M365DSCLogEntry -Message 'Error during Export:' ` -Exception $_ ` -Source $($MyInvocation.MyCommand.Source) ` -TenantId $TenantId ` -Credential $Credential return '' } } $Script:customProperties = @( 'UseManagedBrowser' 'FullScreenEnabled' 'PreComposedIconEnabled' 'IgnoreManifestScope' 'TargetApplicationBundleIdentifier' ) $Script:odataToPropertiesMap = @{ 'iosiPadOSWebClip' = @( 'UseManagedBrowser' 'FullScreenEnabled' 'PreComposedIconEnabled' 'IgnoreManifestScope' 'TargetApplicationBundleIdentifier' ) 'macOSWebClip' = @( 'FullScreenEnabled' 'PreComposedIconEnabled' ) 'webApp' = @( 'UseManagedBrowser' ) 'windowsWebApp' = @() } Export-ModuleMember -Function *-TargetResource |