Skip to content

Instantly share code, notes, and snippets.

@JimMoyle
Last active February 19, 2024 10:27
Show Gist options
  • Save JimMoyle/5826fb2b6bb4652c725b46abc6b46a10 to your computer and use it in GitHub Desktop.
Save JimMoyle/5826fb2b6bb4652c725b46abc6b46a10 to your computer and use it in GitHub Desktop.
function Get-WpmRestApp {
[CmdletBinding()]
Param (
[Parameter(
Position = 0,
ValuefromPipelineByPropertyName = $true,
ValuefromPipeline = $true,
Mandatory = $true
)]
[Alias('Id')]
[System.String]$PackageIdentifier,
[Parameter(
ValuefromPipelineByPropertyName = $true
)]
[System.Uri]$Uri = 'https://pkgmgr-wgrest-pme.azurefd.net/api/packageManifests/',
[Parameter(
ValuefromPipelineByPropertyName = $true
)]
[Int]$ApiTimeoutSeconds = 90
)
begin {
Set-StrictMode -Version Latest
} # begin
process {
Write-Verbose "Querying $PackageIdentifier from $Uri"
$queryUri = $Uri.ToString().TrimEnd('/') + '/' + $PackageIdentifier
$noResponse = $true
$timeSpan = (Get-Date).AddSeconds($ApiTimeoutSeconds)
while ($noResponse) {
if ((Get-Date) -gt $timeSpan) {
Write-Error "Timeout waiting for $PackageIdentifier response from $queryUri"
return
}
try {
$package = Invoke-RestMethod -Uri $queryUri -ErrorAction Stop
$noResponse = $false
}
catch {
Start-Sleep -Milliseconds 500
}
}
if ($null -eq $package) {
Write-Error "Could not find package with Id $PackIdentifier"
return
}
try {
$packageNewest = $package.Data.Versions | Select-Object @{Name = 'PackageVersion'; Expression = { [Version]$_.PackageVersion } }, Installers, DefaultLocale
if (($packageNewest.PackageVersion | Measure-Object).Count -eq 0) {
$packageNewest = $package.Data.Versions | Select-Object @{Name = 'PackageVersion'; Expression = {
$_.PackageVersion -match "(\D{0})(\d+)((\.{1}\d+)*)(\.{0})" | Out-Null
if ($($Matches[0]) -notlike "*.*") {
[Version]($Matches[0] + ".0")
}
else {
[Version]$Matches[0]
}
}
} , Installers, DefaultLocale
}
}
catch {
Write-Output $_
}
$packageDetail = $packageNewest | Sort-Object -Property PackageVersion -Descending | Select-Object -First 1
$timeSpan = (Get-Date).AddSeconds(30)
$appInfo = $null
while (($appInfo | Measure-Object).Count -eq 0 ) {
if ((Get-Date) -gt $timeSpan) {
Write-Error "Timeout waiting for version query match for $PackIdentifier"
return
}
$appInfo = $package.Data.versions | Where-Object { -join $_.Installers.InstallerSha256 -eq -join $packageDetail.Installers.InstallerSha256 }
}
foreach ($installer in $packageDetail.Installers) {
$output = [PSCustomObject]@{
PackageName = $package.Data.PackageIdentifier
PackageVersion = $packageDetail.PackageVersion
Publisher = $appInfo.DefaultLocale.Publisher
ShortDescription = $appInfo.DefaultLocale.ShortDescription
InstallerIdentifier = $installer.InstallerIdentifier
InstallerSha256 = $installer.InstallerSha256
InstallerUrl = $installer.InstallerUrl
Architecture = $installer.Architecture
InstallerType = $installer.InstallerType
Scope = if ($installer.PSobject.Properties.Name -contains 'Scope') { $installer.Scope } else { $null }
InstallerSwitches = if ($installer.PSobject.Properties.Name -contains 'InstallerSwitches') { $installer.InstallerSwitches } else { $null }
Commands = if ($installer.PSobject.Properties.Name -contains 'Commands') { $installer.Commands } else { $null }
InstallerAbortsTerminal = $installer.InstallerAbortsTerminal
ReleaseDate = $installer.ReleaseDate
InstallLocationRequired = $installer.InstallLocationRequired
RequireExplicitUpgrade = $installer.RequireExplicitUpgrade
DisplayInstallWarnings = $installer.DisplayInstallWarnings
DownloadCommandProhibited = $installer.DownloadCommandProhibited
}
Write-Output $output
}
} # process
end {} # end
} #function
@pl4nty
Copy link

pl4nty commented Feb 17, 2024

Is the new REST API ready for public use?

@JimMoyle
Copy link
Author

JimMoyle commented Feb 17, 2024

@pl4nty this API isn't publicly supported no.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment