Private/New-IntunePackage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function New-IntunePackage {
    [CmdletBinding()]
    Param
    (
        [parameter(Position = 0, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$PackageName,
        
        [parameter(Position = 1, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$PackageVersion,
        
        [parameter(Position = 2, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$PackageInstallCmd,

        [parameter(Position = 3, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$PackageUninstallCmd,

        [parameter(Position = 4, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$PackageDetectionPath,

        [parameter(Position = 4, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$PackageDetectionFile,

        [parameter(Position = 5, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$TenantID,

        [parameter(Position = 5, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$InstallerPath


    )
    Write-Verbose "Getting prerequisites $TenantID"
    try {
        Get-Prerequisites
    }
    catch {
        throw "Failed to download stuff: $($_.Exception.Message)"
    }

    try {
        Connect-graphAPI -TenantID $tenantid
    }
    catch {
        throw "Failed to log into Graph API: $($_.Exception.Message)"
    }
    Write-Verbose "Creating new intune package for $TenantID"
    write-verbose "Creating intune package in Endpoint Manager, if $($PackageNames) does not exist"
    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)"
    } 
    
    if ($ApplicationList.count) {
        write-output "The package $($PackageName) already exists, We are removing the existing application."
        Remove-IntunePackage -Packagename $PackageName
    }

    write-verbose "Creating new intunewin package for $($PackageName)"
    $IntuneWinPath = New-IntunewinPackage -InstallerPath $InstallerPath
    write-verbose "Collecting encryption information for $($PackageName)"
    $EncryptionInfo = Get-IntunewinEncryptioninfo -package $IntuneWinPath
    Write-Verbose "Setting application template file."
    $intuneBody = (get-content "$PSScriptRoot\AppTemplate.json") | ConvertFrom-Json
    $intuneBody.DisplayName = $PackageName
    $intuneBody.installcommandline = $PackageInstallCmd
    $intuneBody.uninstallCommandLine = $PackageUnInstallCmd
    $intuneBody.detectionrules[0].path = $PackageDetectionPath
    $intuneBody.detectionRules[0].fileorfoldername = $PackageDetectionFile
    Write-Verbose "Sending package to endpoint manager."
    Send-Intunepackage -package $IntuneWinPath -encryptioninfo $EncryptionInfo -intunebody ($intunebody | ConvertTo-Json)
}