functions/Get-UdeEnvironmentPackage.ps1
|
<# .SYNOPSIS Get UDE environment packages. .DESCRIPTION Gets the UDE environment packages for a specified environment. .PARAMETER EnvironmentId The id of the environment that you want to work against .PARAMETER LatestOnly Instructs the cmdlet to return only the latest package. Is based on the modified date. .PARAMETER AsExcelOutput Instructs the function to export the results to an Excel file. .EXAMPLE PS C:\> Get-UdeEnvironmentPackage -EnvironmentId "env-123" This will retrieve all UDE environment packages for the specified environment id. .EXAMPLE PS C:\> Get-UdeEnvironmentPackage -EnvironmentId "env-123" -LatestOnly This will retrieve only the latest UDE environment package for the specified environment id. It is based on the modified date. .EXAMPLE PS C:\> Get-UdeEnvironmentPackage -EnvironmentId "env-123" -AsExcelOutput This will retrieve all UDE environment packages for the specified environment id. Will output all details into an Excel file, that will auto open on your machine. .NOTES Author: Mötz Jensen (@Splaxi) #> function Get-UdeEnvironmentPackage { [CmdletBinding()] [OutputType('System.Object[]')] param ( [Parameter (Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias("PpacEnvId")] [string] $EnvironmentId, [switch] $LatestOnly, [switch] $AsExcelOutput ) begin { } process { if (Test-PSFFunctionInterrupt) { return } $envObj = Get-UdeEnvironment -EnvironmentId $EnvironmentId -SkipVersionDetails | Select-Object -First 1 if ($null -eq $envObj) { $messageString = "Could not find environment with Id <c='em'>$EnvironmentId</c>. Please verify the Id and try again, or list available environments using <c='em'>Get-UdeEnvironment</c>. Consider using wildcards if needed." Write-PSFMessage -Level Important -Message $messageString Stop-PSFFunction -Message "Stopping because environment was NOT found based on the id." ` -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', ''))) return } $baseUri = $envObj.PpacEnvUri + "/" #! Very important to have the trailing slash $secureToken = (Get-AzAccessToken -ResourceUrl $baseUri -AsSecureString).Token $tokenWebApiValue = ConvertFrom-SecureString -AsPlainText -SecureString $secureToken $headers = @{ "Authorization" = "Bearer $($tokenWebApiValue)" "Accept" = "application/json;odata.metadata=minimal" # minimal || full "OData-MaxVersion" = "4.0" "OData-Version" = "4.0" "Prefer" = "odata.include-annotations=*" } $localUri = $baseUri + "api/data/v9.0/msprov_fnopackages" $colPackages = Invoke-RestMethod -Uri $localUri ` -Method Get ` -Headers $headers | ` Select-Object -ExpandProperty value | ` Sort-Object -Property modifiedon -Descending if ($LatestOnly) { $colPackages = $colPackages | Select-Object -First 1 } $resCol = @( $colPackages | Select-PSFObject -TypeName 'D365Bap.Tools.UdeEnvironmentPackage' ` -Property "msprov_name As Name", "msprov_fnopackageid As PackageId", "'_createdby_value@OData.Community.Display.V1.FormattedValue' As CreatedBy", "'_modifiedby_value@OData.Community.Display.V1.FormattedValue' As ModifiedBy", "'msprov_packagetype@OData.Community.Display.V1.FormattedValue' As PackageType", "'msprov_buildtype@OData.Community.Display.V1.FormattedValue' As BuildType", "'msprov_dbsyncoptions@OData.Community.Display.V1.FormattedValue' As DbSync", "'statuscode@OData.Community.Display.V1.FormattedValue' As Status", "'statecode@OData.Community.Display.V1.FormattedValue' As State", "createdon As Created", "modifiedon As Modified", "versionnumber As Version", * ` -ExcludeProperty '@odata.etag' ) if ($AsExcelOutput) { $resCol | Export-Excel -WorksheetName "Get-UdeEnvironmentPackage" return } $resCol } end { } } |