Module/DevOps/New-BCSDevOpsARMServiceConnection.ps1
<#
.SYNOPSIS Creates a new Service Connection to Azure Resource Manager (ARM) in Azure DevOps. .DESCRIPTION This function creates a new Service Connection to Azure Resource Manager (ARM) in Azure DevOps using the Service Endpoints API. .PARAMETER organization The Azure DevOps organization name. .PARAMETER devOpsPAT The Azure DevOps Personal Access Token. .PARAMETER serviceConnectionName The desired name for the Azure DevOps Service Connection. .PARAMETER subscriptionId The Azure subscription ID. .PARAMETER tenantId The Azure Active Directory (AD) tenant ID. .PARAMETER servicePrincipalId The Azure AD Application (Service Principal) ID. .PARAMETER servicePrincipalKey The secret key associated with the service principal. .EXAMPLE New-BCSARMServiceConnection -organization "YourOrganization" -devOpsPAT "YourAzureDevOpsPAT" ` -serviceConnectionName "YourARMServiceConnection" -subscriptionId "YourSubscriptionId" ` -tenantId "YourTenantId" -servicePrincipalId "YourServicePrincipalId" ` -servicePrincipalKey "YourServicePrincipalKey" #> function New-BCSDevOpsARMServiceConnection { param ( [string]$organization = "BrightComSolutions", [string]$devOpsPAT, [string]$serviceConnectionName = "BCS-Swe-Dev", [string]$subscriptionId, [string]$subscriptionName, [string]$tenantId, [string]$servicePrincipalId, [string]$servicePrincipalKey, [string]$projectName ) $url = "https://dev.azure.com/$organization/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4" $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((":$devOpsPAT"))) $projectId = (Get-BCSDevOpsProject -projectName $projectName -sourcePat (Get-BCSSecureString -InputString $devOpsPAT)).id $hashtable = @{ data = @{ subscriptionId = $subscriptionId subscriptionName = $subscriptionName environment = "AzureCloud" scopeLevel = "Subscription" creationMode = "Manual" } name = $serviceConnectionName type = "AzureRM" url = "https://management.azure.com/" authorization = @{ parameters = @{ tenantid = $tenantId serviceprincipalid = $servicePrincipalId authenticationType = "spnKey" serviceprincipalkey = $servicePrincipalKey } scheme = "ServicePrincipal" } isShared = $false isReady = $true serviceEndpointProjectReferences = @( @{ projectReference = @{ id = $projectId name = $projectName } name = $serviceConnectionName } ) } # To convert to JSON if needed $body = $hashtable | ConvertTo-Json -Depth 10 try { $response = Invoke-RestMethod -Uri $url -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $body -ContentType "application/json" Write-Host "Service Connection created successfully. Connection Id: $($response.id)" } catch { Write-Host "Error creating Service Connection: $($_.Exception)" } } Export-ModuleMember -Function New-BCSDevOpsARMServiceConnection |