|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Retrieves deployment pipeline(s) from Microsoft Fabric. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +The `Get-FabricDeploymentPipeline` function fetches deployment pipeline(s) from the Fabric API. |
| 7 | +It can either retrieve all pipelines or a specific pipeline by ID. |
| 8 | +It automatically handles pagination when retrieving all pipelines. |
| 9 | +
|
| 10 | +.PARAMETER DeploymentPipelineId |
| 11 | +Optional. The ID of a specific deployment pipeline to retrieve. If not provided, all pipelines will be retrieved. |
| 12 | +
|
| 13 | +.PARAMETER DeploymentPipelineName |
| 14 | +Optional. The display name of a specific deployment pipeline to retrieve. If provided, it will filter the results to match this name. |
| 15 | +
|
| 16 | +.EXAMPLE |
| 17 | +Get-FabricDeploymentPipeline |
| 18 | +
|
| 19 | +Retrieves all deployment pipelines that the user can access. |
| 20 | +
|
| 21 | +.EXAMPLE |
| 22 | +Get-FabricDeploymentPipeline -DeploymentPipelineId "GUID-GUID-GUID-GUID" |
| 23 | +
|
| 24 | +Retrieves a specific deployment pipeline with detailed information including its stages. |
| 25 | +
|
| 26 | +.NOTES |
| 27 | +- Calls `Confirm-TokenState` to ensure token validity before making the API request. |
| 28 | +- Returns a collection of deployment pipelines with their IDs, display names, and descriptions. |
| 29 | +- When retrieving a specific pipeline, returns extended information including stages. |
| 30 | +- Requires Pipeline.Read.All or Pipeline.ReadWrite.All delegated scopes. |
| 31 | +
|
| 32 | +Author: Kamil Nowinski |
| 33 | +#> |
| 34 | + |
| 35 | +function Get-FabricDeploymentPipeline { |
| 36 | + [CmdletBinding()] |
| 37 | + param( |
| 38 | + [Parameter(Mandatory = $false)] |
| 39 | + [ValidateNotNullOrEmpty()] |
| 40 | + [Guid]$DeploymentPipelineId, |
| 41 | + |
| 42 | + [Parameter(Mandatory = $false)] |
| 43 | + [Alias("Name", "DisplayName")] |
| 44 | + [string]$DeploymentPipelineName |
| 45 | + ) |
| 46 | + |
| 47 | + try { |
| 48 | + # Step 1: Ensure token validity |
| 49 | + Confirm-TokenState |
| 50 | + |
| 51 | + if ($PSBoundParameters.ContainsKey("DeploymentPipelineName") -and $PSBoundParameters.ContainsKey("DeploymentPipelineId")) |
| 52 | + { |
| 53 | + Write-Warning "The parameters DeploymentPipelineName and DeploymentPipelineId cannot be used together" |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + # If DeploymentPipelineId is provided, get specific pipeline |
| 58 | + if ($DeploymentPipelineId) { |
| 59 | + Write-Message -Message "Retrieving specific deployment pipeline with ID: $DeploymentPipelineId" -Level Debug |
| 60 | + $apiEndpointUrl = "deploymentPipelines/$DeploymentPipelineId" |
| 61 | + |
| 62 | + $response = Invoke-FabricRestMethod -Uri $apiEndpointUrl -Method Get |
| 63 | + |
| 64 | + # Validate response |
| 65 | + Test-FabricApiResponse -response $response -ObjectIdOrName $DeploymentPipelineId -typeName "deployment pipeline" |
| 66 | + |
| 67 | + if ($response) { |
| 68 | + Write-Message -Message "Successfully retrieved deployment pipeline." -Level Debug |
| 69 | + return $response |
| 70 | + } else { |
| 71 | + Write-Message -Message "No deployment pipeline found with the specified ID." -Level Warning |
| 72 | + return $null |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + # Step 2: Initialize variables for listing all pipelines |
| 77 | + $continuationToken = $null |
| 78 | + $pipelines = @() |
| 79 | + |
| 80 | + if (-not ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq "System.Web" })) { |
| 81 | + Add-Type -AssemblyName System.Web |
| 82 | + } |
| 83 | + |
| 84 | + # Step 3: Loop to retrieve all pipelines with continuation token |
| 85 | + Write-Message -Message "Loop started to get continuation token" -Level Debug |
| 86 | + $baseApiEndpointUrl = "deploymentPipelines" |
| 87 | + |
| 88 | + do { |
| 89 | + # Step 4: Construct the API URL |
| 90 | + $apiEndpointUrl = $baseApiEndpointUrl |
| 91 | + |
| 92 | + if ($null -ne $continuationToken) { |
| 93 | + # URL-encode the continuation token |
| 94 | + $encodedToken = [System.Web.HttpUtility]::UrlEncode($continuationToken) |
| 95 | + $apiEndpointUrl = "{0}?continuationToken={1}" -f $apiEndpointUrl, $encodedToken |
| 96 | + } |
| 97 | + Write-Message -Message "API Endpoint: $apiEndpointUrl" -Level Debug |
| 98 | + |
| 99 | + # Step 5: Make the API request |
| 100 | + $response = Invoke-FabricRestMethod -Uri $apiEndpointUrl -Method Get |
| 101 | + |
| 102 | + # Validate response |
| 103 | + Test-FabricApiResponse -response $response -typeName "deployment pipeline" |
| 104 | + |
| 105 | + # Step 6: Process response and update continuation token |
| 106 | + if ($null -ne $response) { |
| 107 | + $pipelines += $response.value |
| 108 | + } |
| 109 | + $continuationToken = Get-FabricContinuationToken -Response $response |
| 110 | + |
| 111 | + } while ($null -ne $continuationToken) |
| 112 | + |
| 113 | + Write-Message -Message "Loop finished and all data added to the list" -Level Debug |
| 114 | + |
| 115 | + if ($DeploymentPipelineName) |
| 116 | + { |
| 117 | + # Filter the list by name |
| 118 | + Write-Message -Message "Filtering deployment pipelines by name: $DeploymentPipelineName" -Level Debug |
| 119 | + $pipelines = $pipelines | Where-Object { $_.displayName -eq $DeploymentPipelineName } |
| 120 | + } |
| 121 | + |
| 122 | + # Step 7: Handle results |
| 123 | + $pipelines |
| 124 | + # if ($pipelines) { |
| 125 | + # Write-Message -Message "Successfully retrieved deployment pipelines." -Level Debug |
| 126 | + # return $pipelines |
| 127 | + # } else { |
| 128 | + # Write-Message -Message "No deployment pipelines found." -Level Warning |
| 129 | + # return $null |
| 130 | + # } |
| 131 | + |
| 132 | + } catch { |
| 133 | + # Step 8: Error handling |
| 134 | + $errorDetails = $_.Exception.Message |
| 135 | + Write-Message -Message "Failed to retrieve deployment pipelines. Error: $errorDetails" -Level Error |
| 136 | + } |
| 137 | +} |
0 commit comments