Created
July 24, 2023 22:22
-
-
Save vinijmoura/e97837b5cf7eee9bdca7adf4ea284b25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param | |
( | |
[string]$PAT, | |
[string]$Organization, | |
[string]$Connstr | |
) | |
$SQLQuery = "TRUNCATE TABLE EnvironmentsApprovalsNames" | |
Invoke-Sqlcmd -query $SQLQuery -ConnectionString $Connstr | |
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) } | |
$UriOrganization = "https://dev.azure.com/$($Organization)/" | |
$uriProject = $UriOrganization + "_apis/projects?`$top=500" | |
$ProjectsResult = Invoke-RestMethod -Uri $uriProject -Method get -Headers $AzureDevOpsAuthenicationHeader | |
Foreach ($project in $ProjectsResult.value) | |
{ | |
$uriEnvironments = $UriOrganization + "$($project.id)/_apis/distributedtask/environments?api-version=6.1-preview.1" | |
$EnvironmentsResult = Invoke-RestMethod -Uri $uriEnvironments -Method get -Headers $AzureDevOpsAuthenicationHeader | |
Foreach ($environment in $EnvironmentsResult.value) | |
{ | |
$body = @( | |
@{ | |
type="queue" | |
id="1" | |
name="Default" | |
}, | |
@{ | |
type="environment" | |
id="$($environment.id)" | |
name="$($environment.name)" | |
} | |
) | ConvertTo-Json | |
$uriEnvironmentChecks = $UriOrganization + "$($project.id)/_apis/pipelines/checks/queryconfigurations?`$expand=settings&api-version=6.1-preview.1" | |
$EnvironmentChecksResult = Invoke-RestMethod -Uri $uriEnvironmentChecks -Method Post -Body $body -Headers $AzureDevOpsAuthenicationHeader -ContentType application/json | |
Foreach ($envcheck in $EnvironmentChecksResult.value) | |
{ | |
if ($envcheck.type.name -eq 'Approval') | |
{ | |
$ApproversResult = Invoke-RestMethod -Uri $envcheck.url -Method get -Headers $AzureDevOpsAuthenicationHeader | |
Foreach ($approver in $ApproversResult.settings.approvers) | |
{ | |
$SQLQuery = "INSERT INTO EnvironmentsApprovalsNames ( | |
TeamProjectName, | |
EnvironmentId, | |
EnvironmentName, | |
ApproverUniqueName, | |
ApproverDisplayName | |
) | |
VALUES( | |
'$($project.name)', | |
$($environment.id), | |
'$($environment.name)', | |
'$($approver.uniqueName)', | |
'$($approver.displayName)' | |
)" | |
Invoke-Sqlcmd -query $SQLQuery -ConnectionString $Connstr | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment