Private/Add-AdditionalEnvironment.ps1

function Invoke-AddEnvironments
{
    $adoRepo = $global:gitRepo.Replace(' ', '')
    $adoOrg = $global:projectFile.ADOOrgName
    $adoProject = $global:projectFile.ADOProject
    
    $message = "Connecting to Power Platform"
    Write-Host $message
    do {
        $sel = Invoke-Menu -MenuTitle "---- Connecting to Power Platform ------" -MenuOptions "Continue", "Quit"
        
     } until ($sel -ge 0)

    if ($sel -eq 1) {
        exit
    }
    
    Install-XrmToolingPowerShell
    
    Write-Host $message
    
    Write-Host ""
    Write-Host "---- Enter the Credentials for your Additional Deployment Environment ------"
    if (!$Credentials) {
        Do {
            $Credentials = Get-Credential -Message "Enter Credentials to Connect to D365 / Power Platform"
        } Until (($Credentials.GetNetworkCredential().UserName -ne "") -and ($Credentials.GetNetworkCredential().Password -ne "")) 
    }
    if (!$username) {
        $username = $Credentials.GetNetworkCredential().UserName
        $password = $Credentials.GetNetworkCredential().Password
    }

    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
    $connCICD = Get-CrmOrganizations -OnLineType OAuth -Credential $Credentials
    $options = $connCICD | ForEach-Object {"$($_.FriendlyName) ($($_.WebApplicationUrl))"}
    
    do {
        $sel = Invoke-Menu -MenuTitle "---- Please Select your Additional Deplyoment Environment ------" -MenuOptions $options
        $CICDEnvironment = $connCICD[$sel]
     } until ($CICDEnvironment -ne "")
     Write-Host $CICDEnvironment.WebApplicationUrl
    
    if (!$Credentials) {
        Do {
            $Credentials = Get-Credential -Message "Enter the Credentials the Pipeline should use for Deploying to D365 / CDS"
        } Until (($Credentials.GetNetworkCredential().UserName -ne "") -and ($Credentials.GetNetworkCredential().Password -ne "")) 
    }
    if (!$username) {
        $username = $Credentials.GetNetworkCredential().UserName
        $password = $Credentials.GetNetworkCredential().Password
    }
    
    $message = "Creating variable groups in Azure DevOps"
    Write-Host $message
    
    try {
        $varGroupCICD = az pipelines variable-group create --organization https://dev.azure.com/$adoOrg --project $adoProject --name "$adoRepo.D365$($CICDEnvironment.UrlHostName)Environment"  --variables d365username=$username --authorize $true | ConvertFrom-Json
        az pipelines variable-group variable create --organization https://dev.azure.com/$adoOrg --project $adoProject --name d365password --value $password --secret $true --group-id $varGroupCICD.id
        az pipelines variable-group variable create --organization https://dev.azure.com/$adoOrg --project $adoProject --name d365url --value $CICDEnvironment.WebApplicationUrl --group-id $varGroupCICD.id
    
        $buildYAML = Get-Content -Path "$global:projectLocation\Build.yaml"
        $azureYAML = Get-Content -Path  (Join-Path $PSScriptRoot ..\Snippets\Environment.yaml)
        $azureYAML = $azureYAML.Replace('environmentName', $CICDEnvironment.FriendlyName)
        $azureYAML = $azureYAML.Replace('replaceRepo', $adoRepo)
        $buildYAML + $azureYAML | Set-Content -Path  "$global:projectLocation\Build.yaml"
       
    }
    catch {
        pause   
    }
    
    
    
}