Functions/DevOps.ServiceConnections.ps1

<#
    .SYNOPSIS
    Get all Azure Resource Manager service connections from Azure DevOps project
 
    .DESCRIPTION
    Get all Azure Resource Manager service connections from Azure DevOps project using Azure DevOps Rest API
 
    .PARAMETER PAT
    Personal Access Token (PAT) for Azure DevOps
 
    .PARAMETER Organization
    Organization name for Azure DevOps
 
    .PARAMETER Project
    Project name for Azure DevOps
 
    .EXAMPLE
    Get-AzDevOpsArmServiceConnections -PAT $PAT -Organization $Organization -Project $Project
#>

function Get-AzDevOpsArmServiceConnections {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $PAT,
        [Parameter()]
        [string]
        $Organization,
        [Parameter()]
        [string]
        $Project
    )
    $header = Get-AzDevOpsHeader -PAT $PAT
    $uri = "https://dev.azure.com/$Organization/$Project/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4"
    $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
    return $response.value | Where-Object { $_.type -eq 'azurerm' }
}
Export-ModuleMember -Function Get-AzDevOpsArmServiceConnections
# End of Function Get-AzDevOpsArmServiceConnections

<#
    .SYNOPSIS
    Get all Checks for service connections from Azure DevOps project
 
    .DESCRIPTION
    Get all Checks for service connections from Azure DevOps project using Azure DevOps Rest API
 
    .PARAMETER PAT
    Personal Access Token (PAT) for Azure DevOps
 
    .PARAMETER Organization
    Organization name for Azure DevOps
 
    .PARAMETER Project
    Project name for Azure DevOps
 
    .PARAMETER ServiceConnectionId
    Service connection id for Azure DevOps
 
    .EXAMPLE
    Get-AzDevOpsArmServiceConnectionChecks -PAT $PAT -Organization $Organization -Project $Project -ServiceConnectionId $ServiceConnectionId
 
    .LINK
    https://learn.microsoft.com/en-us/rest/api/azure/devops/approvalsandchecks/check-configurations/list?view=azure-devops-rest-7.2&tabs=HTTP
#>

function Get-AzDevOpsArmServiceConnectionChecks {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $PAT,
        [Parameter()]
        [string]
        $Organization,
        [Parameter()]
        [string]
        $Project,
        [Parameter()]
        [string]
        $ServiceConnectionId
    )
    $header = Get-AzDevOpsHeader -PAT $PAT
    $uri = "https://dev.azure.com/$Organization/$Project/_apis/pipelines/checks/configurations?api-version=7.2-preview.1&resourceType=endpoint&resourceId=$ServiceConnectionId"
    $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
    return $response.value
}
Export-ModuleMember -Function Get-AzDevOpsArmServiceConnectionChecks
# End of Function Get-AzDevOpsArmServiceConnectionChecks

<#
    .SYNOPSIS
    Export all Azure Resource Manager service connections from Azure DevOps project with checks as nested objects
 
    .DESCRIPTION
    Export all Azure Resource Manager service connections from Azure DevOps project with checks as nested objects using Azure DevOps Rest API
 
    .PARAMETER PAT
    Personal Access Token (PAT) for Azure DevOps
 
    .PARAMETER Organization
    Organization name for Azure DevOps
 
    .PARAMETER Project
    Project name for Azure DevOps
 
    .PARAMETER OutputPath
    Output path for JSON files
 
    .EXAMPLE
    Export-AzDevOpsArmServiceConnections -PAT $PAT -Organization $Organization -Project $Project -OutputPath $OutputPath
 
    .LINK
    https://learn.microsoft.com/en-us/rest/api/azure/devops/approvalsandchecks/check-configurations/list?view=azure-devops-rest-7.2&tabs=HTTP
#>

function Export-AzDevOpsArmServiceConnections {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $PAT,
        [Parameter()]
        [string]
        $Organization,
        [Parameter()]
        [string]
        $Project,
        [Parameter()]
        [string]
        $OutputPath
    )
    $serviceConnections = Get-AzDevOpsArmServiceConnections -PAT $PAT -Organization $Organization -Project $Project
    $serviceConnections | ForEach-Object {
        $serviceConnection = $_
        # Set JSON ObjectType field to Azure.DevOps.ServiceConnection
        $serviceConnection | Add-Member -MemberType NoteProperty -Name ObjectType -Value 'Azure.DevOps.ServiceConnection'
        # Get checks for service connection
        $serviceConnectionChecks = @(Get-AzDevOpsArmServiceConnectionChecks -PAT $PAT -Organization $Organization -Project $Project -ServiceConnectionId $serviceConnection.id)
        $serviceConnection | Add-Member -MemberType NoteProperty -Name Checks -Value $serviceConnectionChecks
        $serviceConnection | ConvertTo-Json -Depth 10 | Out-File "$OutputPath/$($serviceConnection.name).ado.sc.json"
    }
}
Export-ModuleMember -Function Export-AzDevOpsArmServiceConnections
# End of Function Export-AzDevOpsArmServiceConnections