Private/New-Intunewinpackage.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
function New-IntunewinPackage {
    [CmdletBinding()]
    Param
    (
        [parameter(Position = 0, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$InstallerPath
    )
    Write-Verbose "Creating intune package for $($PackageName)"
    $InstallerDirectory = [System.IO.Path]::GetDirectoryName("$($InstallerPath)")
    try { 
        $InstallerFiles = get-childitem $InstallerDirectory
    }
    catch {
        throw "could not get installer path. Did you enter the right path?"
    }
    try {
        $InstallerSize = [math]::round(($InstallerFiles | Measure-Object -Property Length -sum).sum)
        if ($InstallerSize -lt 11MB) {
            Write-Verbose "Adding padding for $($PackageName) as it is under the minimum file size for AzCopy"
            $bytes = 11MB - $InstallerSize
            [System.Security.Cryptography.RNGCryptoServiceProvider] $rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
            $rndbytes = New-Object byte[] $bytes
            $rng.GetBytes($rndbytes)
            [System.IO.File]::WriteAllBytes("$InstallerDirectory\dummy.dat", $rndbytes)
        }
    }
    catch {
        throw "Failed to create padding file. Do we have write permissions?"
    }
    try {
        $FileToExecute = $InstallerPath.split('\') | Select-Object -last 1
        start-process "$env:TEMP\IntuneRMMHelper\IntuneWinAppUtil.exe" -argumentlist "-c $InstallerDirectory -s $FileToExecute -o $InstallerDirectory -q" -wait
    }
    catch {
        throw "Failed to create intunewin file: $($_.Exception.Message)"
    }
    $Returnedfile = get-childitem $InstallerDirectory -Filter *.intunewin | Sort-Object LastWriteTime | Select-Object -Last 1
    return $Returnedfile.fullname
}