-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTest-AzDoServiceConnection.ps1
More file actions
87 lines (75 loc) · 3.01 KB
/
Test-AzDoServiceConnection.ps1
File metadata and controls
87 lines (75 loc) · 3.01 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function Test-AzDoServiceConnection {
<#
.SYNOPSIS
Function to create a service connection in Azure DevOps
.DESCRIPTION
Function to create a service connection in Azure DevOps
.EXAMPLE
$params = @{
CollectionUri = "https://dev.azure.com/contoso"
ProjectName = "Project 1"
SubscriptionId = "00000-00000-00000-00000-00000"
SubscriptionName = "Subscription 1"
Tenantid = "11111-11111-11111-11111-11111"
Serviceprincipalid = "1c03163f-7e4e-4fab-8b41-6f040a8361b9"
KeyVaultName = "kv01"
CertName = "Cert01"
AuthenticationType = "spnCertificate"
ProjectID = "1f31cb4d-5a69-419f-86f0-ee3a8ed9ced2"
Name = "Project 1"
}
Test-AzDoServiceConnection @params
This example creates a new Azure DevOps service connection with a Certificate from a KeyVault in Azure.
#>
[CmdletBinding(SupportsShouldProcess)]
param (
# Name of the Project in Azure DevOps.
[Parameter(Mandatory)]
[string]
$ProjectName,
# Collection Uri. e.g. https://dev.azure.com/contoso.
[Parameter(Mandatory)]
[string]
$CollectionUri,
# Collection Uri. e.g. https://dev.azure.com/contoso.
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[string]
$ServiceConnectionName
)
begin {
$result = @()
Write-Verbose "Starting function: Get-AzDoServiceConnection"
}
process {
$getAzDoServiceConnectionSplat = @{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
ServiceConnectionName = $ServiceConnectionName
}
$connectioninfo = Get-AzDoServiceConnection @getAzDoServiceConnectionSplat
$body = @{
dataSourceDetails = @{
dataSourceName = 'TestConnection'
parameters = $null
}
}
$params = @{
uri = "$CollectionUri/$ProjectName/_apis/serviceendpoint/endpointproxy"
version = "7.2-preview.1"
queryParameters = "endpointId=$($connectioninfo.ServiceConnectionId)"
method = 'POST'
body = $body
}
if ($PSCmdlet.ShouldProcess($ProjectName, "Test service connection on: $($PSStyle.Bold)$ProjectName$($PSStyle.Reset)")) {
$result = Invoke-AzDoRestMethod @params
if ($result.statusCode -eq 'badRequest') {
Write-Error "Connection $($connectioninfo.ServiceConnectionName) is not working: error $($result.errorMessage)"
} else {
[PSCustomObject]@{
Result = "Connection [$($connectioninfo.ServiceConnectionName)] is working as expected"
}
Write-Verbose ($result.result | ConvertFrom-Json -Depth 10 | ConvertTo-Json -Depth 10 -Compress)
}
}
}
}