-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update-ADOTeam.ps1
213 lines (174 loc) · 8.19 KB
/
Update-ADOTeam.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<#
.SYNOPSIS
Updates team settings for a new project team as a self-service solution.
.DESCRIPTION
Updates team settings, that comply with organization Azure DevOps governance.
.PARAMETER Organization
Name of the organization.
.PARAMETER ProjectName
Name of the project where to create a team.
.PARAMETER TeamName
Name of the team to create.
.PARAMETER TeamSettings
Settings of the team to update in JSON format.
.PARAMETER ApiVersion
The API version to use with the Invoke end point; default 7.1-preview.
.EXAMPLE
.\Update-ADOTeam.ps1 `
-Organization $env:MSC365_ORGANIZATION -ProjectName $env:MSC365_PROJECT_NAME `
-TeamName $env:MSC365_TEAM_NAME -TeamSettings $settingsJson -Verbose
The following local/pipeline variables must be set as part of the solution:
$env:MSC365_ORGANIZATION = "msc365"
$env:MSC365_PROJECT_NAME = "az-devops"
$env:MSC365_TEAM_NAME = "Demo Team A"
$env:MSC365_API_VERSION = "7.1-preview"
$env:MSC365_PAT = "{PAT-GOES-HERE}"
The following local/pipeline task environment variables must be set as part of the solution:
$env:AZURE_DEVOPS_EXT_PAT = $env:MSC365_PAT
-----------------------------------------------------------------------------------------------------------------------------------
Script name : Update-DevOpsTeamSettings.ps1
Authors : Martin Swinkels (DevOps Engineer, MSc365.eu Netherlands)
Version : 1.230125.0-beta
Dependencies : az cli, az devops cli
-----------------------------------------------------------------------------------------------------------------------------------
Version Changes:
Date: Version: Changed By: Info:
2023-01-25 1.230125.0-beta Martin Swinkels Renamed file name, fixed verbose and improved error handling.
-----------------------------------------------------------------------------------------------------------------------------------
DISCLAIMER
THIS CODE IS SAMPLE CODE. THESE SAMPLES ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
MICROSOFT FURTHER DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES
OF MERCHANTABILITY OR OF FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK ARISING OUT OF THE USE OR
PERFORMANCE OF THE SAMPLES REMAINS WITH YOU. IN NO EVENT SHALL MICROSOFT OR ITS SUPPLIERS BE LIABLE FOR
ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS
INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
INABILITY TO USE THE SAMPLES, EVEN IF MICROSOFT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
BECAUSE SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR
INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO YOU.
#>
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory = $true)]
[string]$Organization,
[Parameter(Mandatory = $true)]
[string]$ProjectName,
[Parameter(Mandatory = $true)]
[string]$TeamName,
[Parameter(Mandatory = $true)]
[string]$TeamSettings,
[Parameter(Mandatory = $false)]
[string]$ApiVersion = $env:MSC365_API_VERSION
)
# Store current prefernces
$currentWarningPreference = $WarningPreference
$currentVerbosePreference = $VerbosePreference
$currentDebugPreference = $DebugPreference
# Suppress warning messages that can't be avoided.
# $WarningPreference = 'SilentlyContinue'
# Suppress inquire debug message during local testing.
# $DebugPreference = 'Continue'
# Set VerbosePreference to Continue on debug mode.
if (${env:SYSTEM_DEBUG} -eq 'true') {
$VerbosePreference = 'Continue'
$DebugPreference = 'Continue'
# Get-ChildItem -Path env: | Format-Table | Write-Output
}
try {
$fn = "$($PSCmdlet.MyInvocation.MyCommand.Name)"
$st = Get-Date
Write-Verbose @"
`r`n Function...........: $fn
`r Started at ........: $($st.ToString('yyyy-MM-dd hh:mm:ss tt'))
`r VerbosePreference..: $VerbosePreference
`r DebugPreference....: $DebugPreference
`r WarningPreference..: $WarningPreference
"@
$validJson = "$($TeamSettings)" | Test-Json
if (-not $validJson ) {
throw "-TeamSettings input string is not in a valid JSON format."
}
# Configure defaults
az devops configure --defaults `
organization="https://dev.azure.com/$($Organization)" `
project=$ProjectName
Write-Verbose "`r`n Initialized successfully."
$config = az devops configure --list
Write-Debug "`r`n Config: $($config | ConvertTo-Json -Depth 99)"
# Get project
# https://learn.microsoft.com/en-us/cli/azure/devops/project?view=azure-cli-latest#az-devops-project-show
$project = az devops project show --project $ProjectName | ConvertFrom-Json
if (-not $null -eq $project) {
Write-Verbose "`r`n Found project successfully."
Write-Debug ("`r`n project: {0}" -f ($project | ConvertTo-Json -Depth 99))
$team = az devops team show --team $TeamName | ConvertFrom-Json
if(-not $null -eq $team) {
Write-Verbose "`r`n Found team successfully."
Write-Debug ("`r`n team: {0}" -f ($team | ConvertTo-Json -Depth 99))
# Get team settings with az devops invoke
# https://learn.microsoft.com/en-us/cli/azure/devops?view=azure-cli-latest#az-devops-invoke
$settings = az devops invoke `
--area work `
--resource teamsettings `
--route-parameters `
project="$($project.id)" `
team="$($team.id)" `
--http-method GET `
--api-version $ApiVersion `
--output json | ConvertFrom-Json
if (-not $null -eq $settings) {
Write-Verbose "`r`n Found current team settings successfully."
Write-Debug ("`r`n current teamsettings: {0}" -f ($settings | ConvertTo-Json -Depth 99))
$infile = "body.json"
Set-Content -Path $infile -Value $TeamSettings
$newSettings = az devops invoke `
--area work `
--resource teamsettings `
--route-parameters `
project="$($project.id)" `
team="$($team.id)" `
--http-method PATCH `
--in-file $infile `
--api-version $ApiVersion `
--output json | ConvertFrom-Json
Remove-Item $infile -Force
if (-not $null -eq $newSettings) {
Write-Verbose "`r`n Updated team settings successfully."
Write-Debug ("`r`n updated teamsettings: {0}" -f ($newSettings | ConvertTo-Json -Depth 99))
$et = Get-Date
$rt = $et - $st # Run Time
# Format the output time
if ($rt.TotalSeconds -lt 1) {
$elapsed = "$($rt.TotalMilliseconds.ToString('#,0.0000')) Milliseconds"
}
elseif ($rt.TotalSeconds -gt 60) {
$elapsed = "$($rt.TotalMinutes.ToString('#,0.0000')) Minutes"
}
else {
$elapsed = "$($rt.TotalSeconds.ToString('#,0.0000')) Seconds"
}
Write-Verbose @"
`r`n Function...........: $fn
`r Finished at .......: $($et.ToString('yyyy-MM-dd hh:mm:ss tt'))
`r Elapsed time ......: $elapsed
"@
Write-Output -InputObject ($newSettings | ConvertTo-Json -Depth 99)
}
}
}
}
}
catch {
Write-Verbose $_.Exception.Message
throw $_.Exception.Message
}
finally {
# Default preference values for PowerShell are:
# $WarningPreference = 'Continue'
# $VerbosePreference = 'SilentlyContinue'
# $DebugPreference = 'SilentlyContinue'
# Restore preferences.
$WarningPreference = $currentWarningPreference
$VerbosePreference = $currentVerbosePreference
$DebugPreference = $currentDebugPreference
}