generated from libre-devops/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-AzureDevOpsOrg.ps1
73 lines (58 loc) · 1.79 KB
/
Get-AzureDevOpsOrg.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#(./Get-AzureDevOpsOrg.ps1 -OrganizationName $Env:AZP_PROJECT -Pat $Env:AZP_TOKEN).OrganizationId
param (
[Parameter(Mandatory)]
[string]$OrganizationName,
[string]$OrganizationUrl = "https://dev.azure.com/",
[Parameter(Mandatory)]
[string]$Pat,
[string]$Location = "UK South"
)
if (-not(Get-Command az -ErrorAction SilentlyContinue))
{
Write-Error "Azure CLI is not installed. Please install it to continue."
exit
}
$DevOpsOrgUrl = "${OrganizationUrl}${OrganizationName}"
function Get-AzureDevOpsOrgId
{
param (
[string]$OrganizationUrl,
[string]$Pat
)
try
{
# Prepare Authorization Header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$Pat"))
# Define the Azure DevOps REST API endpoint for connection data
$uri = "$OrganizationUrl/_apis/connectionData?api-version=7.0-preview.1"
# Send GET request to Azure DevOps REST API
$response = Invoke-RestMethod -Uri $uri -Headers @{ Authorization = ("Basic {0}" -f $base64AuthInfo) } -Method Get
# Extract and Output the Azure DevOps Organization ID (instanceId)
$organizationId = $response.instanceId.Trim("")
if (-not$response)
{
Write-Error "Failed to obtain the organization ID."
return
}
$orgDetails = New-Object PSObject -Property @{
"OrganizationId" = $organizationId
"OrganizationUrl" = $OrganizationUrl
"OrganizationName" = $OrganizationName
}
return $orgDetails
}
catch
{
Write-Error "Error Getting the DevOps organization ID: $_"
throw $_
}
}
try
{
Get-AzureDevOpsOrgId -OrganizationUrl $DevOpsOrgUrl
}
catch
{
Write-Error "An error occurred: $_"
exit
}