Functions/Get-IntuneWinPackage.psm1

function Get-IntuneWinPackage {
  #Parameters
  [cmdletbinding()]
  param (
      [String]$AppId,
      [String]$Path
  )
  if (!$AppId){
      $AppId = Read-Host "-> App ID"
  }
  if (!$Path){
    $Path = Read-Host "-> Download Path"
  }

  Write-Host -NoNewLine -f Magenta "[Pending] -> Check for updates..."

  $version = (Get-Module -ListAvailable IntuneWinPackage) | Sort-Object Version -Descending  | Select-Object Version -First 1
  $psgalleryversion = Find-Module -Name IntuneWinPackage | Sort-Object Version -Descending | Select-Object Version -First 1
  $stringver = $version | Select-Object @{n='ModuleVersion'; e={$_.Version -as [string]}}
  $a = $stringver | Select-Object Moduleversion -ExpandProperty Moduleversion
  $onlinever = $psgalleryversion | Select-Object @{n='OnlineVersion'; e={$_.Version -as [string]}}
  $b = $onlinever | Select-Object OnlineVersion -ExpandProperty OnlineVersion
  if ([version]"$a" -ge [version]"$b") {
    Write-Host -f DarkGreen "`r[Done] -> Check for updates "
  }
  else {
    Write-Host -f Yellow "`r[Warning] -> This PowerShell module is out of date! Please run Update-Module IntuneWinPackage in a PowerShell session with elevated rights."
    break
  }

  Write-Host -NoNewLine -f Magenta "[Pending] -> Checking parameters..."
  try {
      $directoryInfo = Get-ChildItem $Path | Measure-Object
      if (!$directoryInfo.count -eq 0){
        Write-Host -f Yellow "`r[Warning] -> You path must be empty! "
        break
      }
    }
  catch {
    Write-Host -f Red "`r[Failed] -> Please enter a valid path! "
    break
  }
  Write-Host -f DarkGreen "`r[Done] -> Checking parameters "

  # Set Var
  $temp = $env:TEMP
  $LocalAppdata = $env:LOCALAPPDATA

  Write-Host -NoNewLine -f Magenta "[Pending] -> Checking 7-Zip installation..."
  #Checks
  if (!(Test-Path -Path "C:\Program Files\7-Zip\7z.exe")){
    Write-Host -f Red "`r[Failed] -> No 7-Zip installation detected! "
    Break
  }
  Write-Host -f DarkGreen "`r[Done] -> Checking 7-Zip installation "
  Write-Host -NoNewLine -f Magenta "[Pending] -> Checking config file"
  if (!(Test-Path -Path "$LocalAppdata/IntuneWinPackage/config.json")){
    New-Item -ItemType Directory -Force -Path "$LocalAppdat/IntuneWinPackage" | Out-Null
    New-Item -Path "$LocalAppdata/IntuneWinPackage" -Name "config.json" -Value '{"SASToken": ""}' -Force | Out-Null
  }

  #Config
  $configJSON = Get-Content -Path "$LocalAppdata/IntuneWinPackage/config.json" -raw
  $configPS = ConvertFrom-Json $configJSON

  if(!$configPS.SASToken){
    Write-Host -f Yellow "`r[Warning] -> No SAS Token set! "
    $configPS.SASToken = Read-Host "SAS Token: "
    $configJSON = ConvertTo-Json -depth 32 $configPS
    $configJSON | Out-File "$LocalAppdata/IntuneWinPackage/config.json"
  }
  Write-Host -f DarkGreen "`r[Done] -> Checking config file "

  #Set Alias
  set-alias sz "C:\Program Files\7-Zip\7z.exe"

  $TableHeader = @{
      'Accept' = 'application/json;odata=nometadata'
      }
  $TableURL = "https://psgaintuneuploadtable.table.core.windows.net/uploads(RowKey='"+$AppId+"',PartitionKey='IntuneWin')"+($configPS.SASToken)

  Write-Host -NoNewLine -f Magenta "[Pending] -> Searching database..."

  try {
    $PackagePS = Invoke-RestMethod -Method "GET" -Uri $TableURL -Header $TableHeader
  }
  catch {
    Write-Host -f Red "`r[Failed] -> No entity in database found!"
    break
  }
  Write-Host  -f DarkGreen "`r[Done] -> Searching database "

  $EncryptionKey = $PackagePS.EncryptionKey
  $InitializationVector = $PackagePS.InitializationVector

  $source = ($PackagePS.IntuneWinURI)
  $destination = "$temp/Application.intunewin.bin"

  Write-Host -NoNewLine -f Magenta "[Pending] -> Download IntuneWin..."

  try {
    Start-BitsTransfer -Source $source -Destination $destination | Out-Null
    Write-Host -f DarkGreen "`r[Done] -> Download IntuneWin "
  }
  catch {
    Write-Host -f Red "`r[Failed] -> Download failed! "
    break
  }

  Write-Host -NoNewLine -f Magenta "[Pending] -> Decode IntuneWin..."

  try {
    &"$PSScriptRoot/IntuneWinAppUtilDecoder.exe" "$temp/Application.intunewin.bin" /key:"$EncryptionKey" /iv:"$InitializationVector" | Out-Null
    Write-Host -f DarkGreen "`r[Done] -> Decode IntuneWin "  
  }
  catch {
    Write-Host -f Red "`r[Failed] -> Decoding failed! "
    break
  }

  Write-Host -NoNewLine -f Magenta "[Pending] -> UnZip decoded IntuneWin..."

  try {
    sz x "$temp/Application.intunewin.bin.decoded" -o"$Path" | Out-Null
    Write-Host -f DarkGreen "`r[Done] -> UnZip decoded IntuneWin "
  }
  catch {
    Write-Host -f Red "`r[Failed] -> UnZipping failed! "
    break
  }

  Write-Host -NoNewLine -f Magenta "[Pending] -> CleanUp..."

  try {
    Remove-Item "$temp/Application.intunewin.bin"
    Remove-Item "$temp/Application.intunewin.bin.decoded"
    Write-Host -f DarkGreen "`r[Done] -> CleanUp "
  }
  catch {
    Write-Host -f Red "`r[Failed] -> CleanUp failed! "
    break
  }
}