functions/Get-PpacFinOpsBuild.ps1
|
<# .SYNOPSIS Lists quality updates for a FinOps track. .DESCRIPTION Queries the developer tools service for every published build of one application track. .PARAMETER AppVersion Track to list as version, build or full build. Omitted: newest track. .PARAMETER Latest Returns only the newest build. .PARAMETER EnvironmentId Environment for the auth context only. Omitted: first FinOps environment is used. .PARAMETER AsExcelOutput Instructs the cmdlet to output the results as an Excel file. .EXAMPLE PS C:\> Get-PpacFinOpsBuild -AppVersion "10.0.49" It lists all builds of the 10.0.49 track. It uses -AppVersion "10.0.49" to choose the track. It uses -EnvironmentId when omitted for the first FinOps environment. .EXAMPLE PS C:\> Get-PpacFinOpsVersion -Latest | Get-PpacFinOpsBuild -Latest It lists the newest build of the newest track. It uses -Latest to return only the newest build. It uses -EnvironmentId when omitted for the first FinOps environment. .EXAMPLE PS C:\> Get-PpacFinOpsBuild -AsExcelOutput It lists all builds of the newest track into an Excel file. It uses -AsExcelOutput to write the results into an Excel file. It uses -EnvironmentId when omitted for the first FinOps environment. .NOTES Author: Mötz Jensen (@Splaxi) #> function Get-PpacFinOpsBuild { [CmdletBinding()] [OutputType('D365Bap.Tools.PpacFinOpsBuild[]')] param ( [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $AppVersion, [switch] $Latest, [Parameter(ValueFromPipelineByPropertyName = $true)] [Alias("PpacEnvId")] [string] $EnvironmentId, [switch] $AsExcelOutput ) process { $hashCtx = Get-DevToolsAuthContext -EnvironmentId $EnvironmentId if ($null -eq $hashCtx) { return } $hashHeaders = @{ "Correlationid" = [guid]::NewGuid().ToString() "Dataverseenvironmenturi" = $hashCtx.Env.PpacEnvUri "Authorization" = "Bearer $($hashCtx.Token)" "Odata-Maxversion" = "4.0" "Odata-Version" = "4.0" "Accept" = "application/json" } try { $catalog = Invoke-RestMethod -Method Get ` -Uri "https://developertools.powerplatform.microsoft.com/api/clientmetadata/finopsversions" ` -Headers $hashHeaders } catch { $messageString = "Failed retrieving the FinOps version catalog from the developer tools service." Write-PSFMessage -Level Important ` -Message $messageString ` -ErrorRecord $_ Stop-PSFFunction -Message "Stopping because the finopsversions request failed." ` -ErrorRecord $_ return } $track = $null $trackAppVersion = $null if ([string]::IsNullOrWhiteSpace($AppVersion)) { $objEntry = $catalog | Sort-Object -Property { [version]$_.appVersion } | Select-Object -Last 1 if ($null -eq $objEntry) { Stop-PSFFunction -Message "Stopping because the version catalog is empty." return } $track = $objEntry.appBuild $trackAppVersion = $objEntry.appVersion } else { $normalized = "$AppVersion".Replace("-", ".").Trim() $objAppVersionMatch = $catalog | Where-Object { $_.appVersion -eq $normalized } | Select-Object -First 1 if (-Not ($null -eq $objAppVersionMatch)) { $track = $objAppVersionMatch.appBuild $trackAppVersion = $objAppVersionMatch.appVersion } else { $parts = $normalized -split "\." if ($parts.Count -eq 4) { $candidate = "$($parts[0]).$($parts[1]).$($parts[2])" $objBuildMatch = $catalog | Where-Object { $_.appBuild -eq $candidate } | Select-Object -First 1 if (-Not ($null -eq $objBuildMatch)) { $track = $objBuildMatch.appBuild $trackAppVersion = $objBuildMatch.appVersion } } elseif ($parts.Count -eq 3) { $objBuildMatch = $catalog | Where-Object { $_.appBuild -eq $normalized } | Select-Object -First 1 if (-Not ($null -eq $objBuildMatch)) { $track = $objBuildMatch.appBuild $trackAppVersion = $objBuildMatch.appVersion } } if ([string]::IsNullOrWhiteSpace($track)) { $colValid = @( $catalog | Sort-Object -Property { [version]$_.appVersion } | ForEach-Object { $_.appVersion } ) $valid = $colValid -join ", " $messageString = "Unknown application track <c='em'>$AppVersion</c>. Valid values are: $valid. Application builds ($($catalog[0].appBuild) style) and full builds are also accepted." Write-PSFMessage -Level Important ` -Message $messageString Stop-PSFFunction -Message "Stopping because the application track was not recognized." ` -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', ''))) return } } } $trackDashed = $track.Replace(".", "-") $requestUri = 'https://developertools.powerplatform.microsoft.com/api/clientmetadata/finopsbuilds?version={0}' ` -f $trackDashed try { $rawBuilds = Invoke-RestMethod -Method Get ` -Uri $requestUri ` -Headers $hashHeaders } catch { $messageString = "Failed retrieving FinOps builds for track <c='em'>$track</c> from the developer tools service." Write-PSFMessage -Level Important ` -Message $messageString ` -ErrorRecord $_ Stop-PSFFunction -Message "Stopping because the finopsbuilds request failed." ` -ErrorRecord $_ return } if (($null -eq $rawBuilds) ` -or (@($rawBuilds).Count -eq 0)) { $messageString = "No quality updates found for track <c='em'>$track</c>." Write-PSFMessage -Level Important ` -Message $messageString Stop-PSFFunction -Message "Stopping because no builds were returned." ` -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', ''))) return } $colRes = @( $rawBuilds | Sort-Object -Property { [version]$_ } ` -Descending | Select-PSFObject ` -TypeName "D365Bap.Tools.PpacFinOpsBuild" ` -Property ` @{Name = "AppVersion"; Expression = { $trackAppVersion } }, @{Name = "AppBuild"; Expression = { $track } }, @{Name = "Track"; Expression = { $track } }, @{Name = "Build"; Expression = { "$_" } } ) Write-FinOpsVersionCache ` -Versions $catalog ` -Builds @{$track = @($rawBuilds) } $output = if ($Latest) { $colRes | Select-Object -First 1 } else { $colRes } if ($AsExcelOutput) { $output | Export-Excel -WorksheetName "Get-PpacFinOpsBuild" ` -NoNumberConversion AppVersion, AppBuild, Track, Build return } $output } } |