Private/Remove-IntunePackage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function Remove-IntunePackage {
    [CmdletBinding()]
    Param
    (
        [parameter(Position = 0, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$PackageName
    )
    try {
        $Applicationlist = (Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps" -Headers $script:GraphHeader -Method get -ContentType "application/json").value | where-object { $_.DisplayName -eq $Packagename }
    }
    catch {
        throw "Error: Could not connect to API to retrieve current packages: $($_.Exception.Message)"
    } 

    foreach ($Application in $applicationlist) {
        write-verbose "Removing application $($application.name)"
        (Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps/$($Application.id)" -Headers $script:GraphHeader -Method DELETE -ContentType "application/json")

    }
}