Public/Get-NetExtender.ps1

<#
.SYNOPSIS
Fetches the latest version of NetExtender from the SonicWall API and downloads it.
 
.DESCRIPTION
This script fetches the list of available software from the SonicWall API,
finds the latest version of NetExtender, and downloads the MSI for Windows 64-bit.
 
.PARAMETER Path
The path where the downloaded file will be saved.
 
.EXAMPLE
PS C:\> Get-NetExtender.ps1 -Path "C:\Downloads"
 
This will download the latest version of NetExtender and save it to the "C:\Downloads" directory.
 
.EXAMPLE
PS C:\> Get-NetExtender.ps1
 
This will download the latest version of NetExtender and save it to the $env:temp directory.
 
.NOTES
Make sure you have internet connectivity and sufficient permissions to save files to the specified path.
 
#>


function Get-NetExtender {
  [CmdletBinding(SupportsShouldProcess)]
  param(
    [string]$Path = "$env:temp"
  )

  # Base URL for NetExtender downloads
  $SourceBase = "https://software.sonicwall.com/NetExtender"

  # Fetch the list of available software from the SonicWall API
  $NEResponse = Invoke-WebRequest -UseBasicParsing -Uri "https://api.mysonicwall.com/api/downloads/get-freedownloads" `
  -Method "POST" `
  -Headers @{
    "authority"="api.mysonicwall.com"
    "method"="POST"
    "path"="/api/downloads/get-freedownloads"
    "scheme"="https"
    "accept"="application/json, text/plain, */*"
  } `
  -ContentType "application/json" `
  -Body "{`"category`":`"LATEST`",`"productType`":`"17491`",`"swLangCode`":`"EN`",`"fileType`":[`"Firmware`"],`"releaseTypeList`":[{`"releaseType`":`"ALL`"}],`"keyWord`":`"`",`"previousVersions`":false,`"isFileTypeChange`":false,`"username`":`"ANONYMOUS`"}"

  # Convert the JSON response to a PowerShell object
  $NEJson = ConvertFrom-JSON $NEResponse.Content

  # Get the latest version of NetExtender
  $NELatest = $NEJson.content.UserDownloads[0].applicableDownloads[0]

  # Check that the latest version is actually NetExtender
  if($NELatest.Name -ne "NetExtender") {
    Write-Error "Something's not right, our download is not for NetExtender"
    exit 1
  }

  # Get the list of downloads for the latest version
  $NEDownloads = $NELatest.softwareList

  # Get the MSI for Windows 64-bit
  $NEWin64 = $NEDownloads | Where-Object name -like "*Windows 64 bit*"


  if ($PSCmdlet.ShouldProcess("NetExtender MSI", "Download")) {
    # Download the latest version
    try {
      Invoke-WebRequest -UseBasicParsing "$SourceBase/NetExtender-x64-$($NEWin64.Version).msi" -OutFile "$Path\NetExtender-x64-$($NEWin64.Version).msi"
    } catch {
      Write-Error "Download error or version mismatch"
    }
  }

  $DestinationFilePath = "$Path\NetExtender-x64-$($NEWin64.Version).msi"

  if ($PSCmdlet.ShouldProcess("NetExtender MSI", "Verify download")) {
      
    # Verify the download against the provided MD5 hash
    $DownloadedHash = (Get-FileHash $DestinationFilePath -Algorithm MD5).Hash | Out-String
    $DownloadHash = $NEWin64.md5HashValue | Out-String

    Write-Verbose "Downloaded hash: $DownloadedHash"
    Write-Verbose "Target hash: $DownloadHash"
    Write-Verbose "Destination File Path: $DestinationFilePath"

    if($DownloadHash.ToUpper() -eq $DownloadedHash.ToUpper()) {
      Write-Verbose "Downloaded installer hash matches developer hash."
      Return Get-Item -Path $DestinationFilePath
      
    } else {
      Write-Error "Invalid installer download."
    }

  }

}

Export-ModuleMember -Function Get-NetExtender