-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureDevOpsNotificationBanners.psm1
More file actions
736 lines (598 loc) · 27.6 KB
/
Copy pathAzureDevOpsNotificationBanners.psm1
File metadata and controls
736 lines (598 loc) · 27.6 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
################################################################################
# Configuration section
# Put general configuration stuff and validations here
################################################################################
#Requires -Version 6.2
Set-StrictMode -Version 3.0
$scriptName = "Azure DevOps Notification Banner Administration"
# Use semantic versioning here
$version = "1.1.0"
$year = "2020"
$Global:quietAnswer = $true
$Global:defaultAnswer = $false
$RestCallRetryDelayInSeconds = 2
function Test-Parameters-New-NotificationBanner()
{
Write-Host " Type: $NotificationType"
Write-Host " Message: $Message"
}
function Test-Parameters-Get-NotificationBanners()
{
# Nothing to check
}
function Test-Parameters-Remove-NotificationBanner()
{
Write-Host " ID: $BannerId"
}
function Test-Parameters-Clear-NotificationBanners()
{
# Nothing to check
}
function Test-Parameters([string] $command)
{
$validationResult = $true;
Write-Host " Command: $command"
Write-Host " Service: $ServiceUri" -NoNewline
if (!([System.Uri]::IsWellFormedUriString($ServiceUri, [System.UriKind]::Absolute)))
{
Write-Host " (invalid)" -ForegroundColor Red -NoNewline
$validationResult = $false
}
Write-Host
&"Test-Parameters-$command"
Write-Host "Authentication: $(if ($UsePAT) { "PAT" } elseif ($GetCredentials) { "Custom Credentials" } else { "Default Credentials" })"
$Global:isQuiet = $Quiet
Write-Host " Quiet: $Quiet"
Write-Host " Max Retries: $MaxRestCallRetries" -NoNewline
if ($MaxRestCallRetries -lt 1) {
Write-Host " (invalid)" -ForegroundColor Red -NoNewline
$validationResult = $false;
}
Write-Host
Write-Host " Trace Errors: $TraceErrors"
Write-Host "--------------------------------------------------------------------------------"
return $validationResult
}
################################################################################
# Helper functions
################################################################################
function Write-Header()
{
Write-Host "--------------------------------------------------------------------------------"
Write-Host "$scriptName - v$version"
Write-Host "Copyright (c) $year Microsoft Premier Services - Microsoft Deutschland GmbH"
Write-Host "--------------------------------------------------------------------------------"
}
function Write-SimpleError($message)
{
Write-Host $message -ForegroundColor red -BackgroundColor black
}
function Exit-WithError($message, $exitCode)
{
Write-SimpleError $message
exit $exitCode
}
function Get-Consent([string] $message)
{
if ($Global:isQuiet)
{
return $Global:quietAnswer
}
Write-Host "$message"
if ($Global:defaultAnswer) {
Write-Host "[Y] Yes" -ForegroundColor Yellow -NoNewline
Write-Host " [A] Yes to All [N] No [L] No to All (default is `"Y`"): " -NoNewline
}
else
{
Write-Host "[Y] Yes [A] Yes to All " -NoNewline
Write-Host "[N] No" -ForegroundColor Yellow -NoNewline
Write-Host " [L] No to All (default is `"N`"): " -NoNewline
}
switch (Read-Host)
{
"y" { $true }
"a" { $Global:quietAnswer = $true; $Global:isQuiet = $true; return $true }
"n" { return $false }
"l" { $Global:quietAnswer = $false; $Global:isQuiet = $true; return $false }
default { return $Global:defaultAnswer }
}
}
function Get-Encoding($encodingString)
{
switch($encodingString.ToLower())
{
"ascii" { return [System.Text.Encoding]::ASCII }
"unicode" { return [System.Text.Encoding]::Unicode }
"utf7" { return [System.Text.Encoding]::UTF7 }
"utf8" { return [System.Text.Encoding]::UTF8 }
"utf32" { return [System.Text.Encoding]::UTF32 }
}
}
function ConvertTo-Base64String
{
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline)]
[string] $InputString,
[ValidateSet("ascii", "unicode", "utf7", "utf8", "utf32")]
[string] $Encoding
)
Process {
$enc = Get-Encoding $Encoding
$bytes = $enc.GetBytes($inputString)
return [System.Convert]::ToBase64String($bytes)
}
}
Function ConvertFrom-Base64String
{
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline)]
[string] $InputString,
[ValidateSet("ascii", "unicode", "utf7", "utf8", "utf32")]
[string] $Encoding
)
Process {
$enc = Get-Encoding $Encoding
$bytes = [System.Convert]::FromBase64String($inputString)
return $enc.GetString($bytes)
}
}
function Test-Elevation()
{
# https://gist.github.com/jhochwald/46014a3de425dc21c1f1f7e31cd49cf1
return ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')
}
function Assert-Preconditions([string] $command)
{
Write-Header
if (!(Test-Parameters -command $command)) { Exit-WithError "One or more input parameters are invalid. Aborting." -1 }
Get-HttpHeadersAndCredentials
}
################################################################################
# REST call functions
################################################################################
# HTTP Headers
$Global:headers = @{}
$Global:credentials = $null
function Get-HttpHeadersAndCredentials()
{
$Global:headers = @{ Accept="application/json" }
$Global:headers["Accept-Charset"] = "utf-8"
if ($UsePAT)
{
$PAT = Read-Host "Enter PAT" -AsSecureString
$patString = "pat:$([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PAT)))"
$Global:headers["Authorization"] = "Basic $(ConvertTo-Base64String -InputString $patString -Encoding ascii)"
}
if ($GetCredentials)
{
$Global:credentials = Get-Credential
}
}
function Get-ErrorForWebException([System.Net.Http.HttpRequestException] $Exception, [string] $reqBody)
{
try
{
# see https://stackoverflow.com/questions/35986647/how-do-i-get-the-body-of-a-web-request-that-returned-400-bad-request-from-invoke
$respStream = $Exception.Response.GetResponseStream()
$respStream.Position = 0
$reader = New-Object System.IO.StreamReader($respStream)
$respBody = $reader.ReadToEnd()
$respStatusCode = $Exception.Response.StatusCode
$respStatusCodeInt = [System.Convert]::ToInt32($respStatusCode)
return "Status Code $respStatusCode ($respStatusCodeInt): $respBody `n $reqBody"
}
catch
{
return $(if ($Exception) { $Exception.ToString() } else { "Unknown Error." })
}
}
function Invoke-RestGet($uri, [ref]$responseHeader)
{
Invoke-Rest $uri "Get" ([ref]$responseHeader)
}
function Invoke-RestPost($uri, $body, [ref]$responseHeader)
{
Invoke-RestWithBody $uri "Post" $body ([ref]$responseHeader)
}
function Invoke-RestPut($uri, $body, [ref]$responseHeader)
{
Invoke-RestWithBody $uri "Put" $body ([ref]$responseHeader)
}
function Invoke-RestPatch($uri, $body, [ref]$responseHeader)
{
Invoke-RestWithBody $uri "Patch" $body ([ref]$responseHeader)
}
function Invoke-RestDelete($uri, [ref]$responseHeader)
{
Invoke-Rest $uri "Delete" ([ref]$responseHeader)
}
function Invoke-RestOptions($uri, [ref]$responseHeader)
{
Invoke-Rest $uri "Options" ([ref]$responseHeader)
}
function Invoke-RestWithRetriesAndTracing($uri, $method, $body = $null, [ref]$responseHeader)
{
$success = $false
$tries = 0
$delaySeconds = $RestCallRetryDelayInSeconds
while (-not $success -and $tries -lt $MaxRestCallRetries)
{
$isLastTry = ($tries -eq ($MaxRestCallRetries - 1))
if ($isLastTry -and $TraceErrors)
{
if (Test-Elevation)
{
$traceFilePath = [System.IO.Path]::Combine($env:TEMP, "Invoke-Rest$method-$((Get-Date).Ticks.ToString()).etl")
Write-Host "Tracing last try for $uri at $traceFilePath..."
Invoke-Command -ScriptBlock {netsh trace start persistent=yes capture=yes tracefile="$traceFilePath"}
}
else
{
Write-Warning "Cannot create a network trace for communication with URI $uri. Please run the script as local administrator."
}
}
$tries += 1
try
{
if ($null -eq $body)
{
$result = Invoke-Rest $uri $method ([ref]$responseHeaderValue)
}
else
{
$result = Invoke-RestWithBody $uri $method $body ([ref]$responseHeaderValue)
}
$success = $true
}
catch
{
if ($_.Exception.GetType().FullName -eq "System.Net.Http.HttpRequestException")
{
$exceptionMessage = GetErrorForWebException -Exception $_.Exception
}
else
{
$exceptionMessage = $_.Exception.Message
}
$message = "Call failed with $exceptionMessage."
if ($isLastTry)
{
Exit-WithError $message 1
}
else
{
Write-Warning $message
Write-Host "Retrying after $delaySeconds seconds..." -ForegroundColor Yellow
Start-Sleep -Seconds $delaySeconds
$delaySeconds *= 2 # Exponential Backoff
}
}
finally
{
if ($isLastTry -and $TraceErrors -and (Test-Elevation)) {
Write-Host "Finishing network trace. This will take a while... Please do not interrupt!"
Invoke-Command -ScriptBlock {netsh trace stop}
if ($success)
{
Remove-Item $traceFilePath
}
}
}
}
return $result
}
function Invoke-Rest($uri, $method, [ref]$responseHeader)
{
$responseHeaderValue = @{}
if ($UsePAT)
{
return Invoke-RestMethod $uri -Method $method -Headers $Global:headers -ResponseHeadersVariable "responseHeaderValue"
}
elseif ($GetCredentials)
{
return Invoke-RestMethod $uri -Method $method -Headers $Global:headers -Credential $Global:credentials -ResponseHeadersVariable "responseHeaderValue"
}
else
{
return Invoke-RestMethod $uri -Method $method -Headers $Global:headers -UseDefaultCredentials -ResponseHeadersVariable "responseHeaderValue"
}
if ($responseHeader) {
$responseHeader.Value = $responseHeaderValue
}
}
function Invoke-RestWithBody($uri, $method, $body, [ref]$responseHeader)
{
$jsonBody = ConvertTo-Json $body -Depth 100 -Compress
$jsonBody = $jsonBody.Replace("\u0026", "&")
$responseHeaderValue = @{}
if ($UsePAT)
{
return Invoke-RestMethod $uri -Method $method -Headers $Global:headers -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($jsonBody)) -ResponseHeadersVariable "responseHeaderValue"
}
elseif ($GetCredentials)
{
return Invoke-RestMethod $uri -Method $method -Headers $Global:headers -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($jsonBody)) -Credential $Global:credentials -ResponseHeadersVariable "responseHeaderValue"
}
else
{
return Invoke-RestMethod $uri -Method $method -Headers $Global:headers -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($jsonBody)) -UseDefaultCredentials -ResponseHeadersVariable "responseHeaderValue"
}
if ($responseHeader) {
$responseHeader.Value = $responseHeaderValue
}
}
################################################################################
# Business logic functions
################################################################################
function Get-ApiVersion()
{
$optionsUri = "$ServiceUri/_apis/settings"
$availableApis = Invoke-RestOptions $optionsUri
$entriesApis = $availableApis.value | Where-Object -Property resourceName -eq entries
if ($null -ne $entriesApis)
{
if ($entriesApis[0].releasedVersion -ne "0.0")
{
return "api-version=$($entriesApis[0].releasedVersion)"
}
else
{
return "api-version=$($entriesApis[0].maxVersion)-preview"
}
}
Exit-WithError -message "Notification Banners are not suppoted!" -exitCode 100
}
################################################################################
# Exported functions
################################################################################
<#
.SYNOPSIS
New-NotificationBanner
.DESCRIPTION
Create notification banners for Azure DevOps.
.PARAMETER ServiceUri
Provide the URI of the Azure DevOps Services organization or Azure DevOps Server collection you want to work on.
.PARAMETER NotificationType
Specify the notification type. This can be either Information, Warning, or Error.
.PARAMETER Message
pecify the notification message.
.PARAMETER Quiet
Specify this switch to suppress confirmation prompts.
.PARAMETER GetCredentials
Specify this switch if you need to use special credentials when connecting to your Azure DevOps Server.
.PARAMETER UsePAT
Specify this switch if you need to provide a Personal Access Token to connect your Azure DevOps Server/Services.
.PARAMETER MaxRestCallRetries
Provide the number of retries for failing REST calls. In general, you shouldn't need this option. However, when running in an
unstable environment (e.g., unreliable network connection), retries can help by automatically rerunning failing REST calls.
.PARAMETER TraceErrors
Specify this switch to enabled extended error tracing using netsh. If you combine this with the MaxRestCallRetries parameter,
only the last retry is traced. Otherwise, every request is trace, but traces for succeeding network calls are deleted for security reasons.
Note: You need to run the script as a local administrator when using this switch. Otherwise, network tracing is not allowed.
.EXAMPLE
.\New-NotificationBanner.ps1 -ServiceUri http://MyTfs:8080/tfs/DefaultCollection -NotificationType Information -Message "Happy holiday season to all!" -GetCredentials
Creates a new information banner in the DefaultCollection of your local Azure DevOps Server using Windows credentials.
.EXAMPLE
.\New-NotificationBanner.ps1 -ServiceUri https://dev.azure.com/myOrg -NotificationType Warning -Message "There are currently issues with our license distribution. Please contact the help desk if you are missing licensed features." -Quiet -UsePAT
Creates a new warning banner in the Azure DevOps Services organization myOrg, suppressing confirmations and using a PAT for authentication.
#>
function New-NotificationBanner() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, HelpMessage="Enter the URI of the Azure DevOps Services organization or Azure DevOps Server collection you want to work on.")]
[ValidateNotNullOrEmpty()]
[string]$ServiceUri,
[Parameter(Mandatory=$true, HelpMessage="Specify the notification type. This can be either Information, Warning, or Error.")]
[ValidateSet("Information", "Warning", "Error")]
[string]$NotificationType,
[Parameter(Mandatory=$true, HelpMessage="Specify the notification message.")]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch to suppress confirmation prompts.")]
[switch]$Quiet,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch if you need to use special credentials when connecting to your Azure DevOps Server.")]
[switch]$GetCredentials,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch if you need to provide a Personal Access Token to connect your Azure DevOps Server/Services.")]
[switch]$UsePAT,
[Parameter(Mandatory=$false, HelpMessage="Provide the number of retries for failing REST calls. In general, you shouldn't need this option.")]
[int]$MaxRestCallRetries = 1,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch to enable extended error tracing using netsh.")]
[switch]$TraceErrors
)
Assert-Preconditions -command New-NotificationBanner
if (!(Get-Consent -message "Do you want to create this new notification?"))
{
Write-Host "Aborted."
exit 0
}
$apiVersion = Get-ApiVersion
$uri = "$ServiceUri/_apis/settings/entries/host?$apiVersion"
$messageId = [Guid]::NewGuid()
$body = @{
"GlobalMessageBanners/$messageId" = @{
"level" = $NotificationType
"message" = $Message
}
}
Invoke-RestPatch -uri $uri -body $body
Write-Host "Notification banner with ID $messageId created."
}
<#
.SYNOPSIS
Get-NotificationBanners
.DESCRIPTION
Lists all existing notification banners for Azure DevOps.
.PARAMETER ServiceUri
Provide the URI of the Azure DevOps Services organization or Azure DevOps Server collection you want to work on.
.PARAMETER Quiet
Specify this switch to suppress confirmation prompts.
.PARAMETER GetCredentials
Specify this switch if you need to use special credentials when connecting to your Azure DevOps Server.
.PARAMETER UsePAT
Specify this switch if you need to provide a Personal Access Token to connect your Azure DevOps Server/Services.
.PARAMETER MaxRestCallRetries
Provide the number of retries for failing REST calls. In general, you shouldn't need this option. However, when running in an
unstable environment (e.g., unreliable network connection), retries can help by automatically rerunning failing REST calls.
.PARAMETER TraceErrors
Specify this switch to enabled extended error tracing using netsh. If you combine this with the MaxRestCallRetries parameter,
only the last retry is traced. Otherwise, every request is trace, but traces for succeeding network calls are deleted for security reasons.
Note: You need to run the script as a local administrator when using this switch. Otherwise, network tracing is not allowed.
.EXAMPLE
.\Get-NotificationBanners.ps1 -ServiceUri http://MyTfs:8080/tfs/DefaultCollection -GetCredentials
Lists all notification banners in the DefaultCollection of your local Azure DevOps Server using Windows credentials.
.EXAMPLE
.\Get-NotificationBanners.ps1 -ServiceUri https://dev.azure.com/myOrg -UsePAT
Lists all notification banners in the Azure DevOps Services organization myOrg using a PAT for authentication.
#>
function Get-NotificationBanners() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, HelpMessage="Enter the URI of the Azure DevOps Services organization or Azure DevOps Server collection you want to work on.")]
[ValidateNotNullOrEmpty()]
[string]$ServiceUri,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch to suppress confirmation prompts.")]
[switch]$Quiet,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch if you need to use special credentials when connecting to your Azure DevOps Server.")]
[switch]$GetCredentials,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch if you need to provide a Personal Access Token to connect your Azure DevOps Server/Services.")]
[switch]$UsePAT,
[Parameter(Mandatory=$false, HelpMessage="Provide the number of retries for failing REST calls. In general, you shouldn't need this option.")]
[int]$MaxRestCallRetries = 1,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch to enable extended error tracing using netsh.")]
[switch]$TraceErrors
)
Assert-Preconditions -command Get-NotificationBanners
$apiVersion = Get-ApiVersion
$uri = "$ServiceUri/_apis/settings/entries/host/GlobalMessageBanners?$apiVersion"
$banners = Invoke-RestGet -uri $uri
Write-Host "Found $($banners.count) notification banner(s):"
if ($banners.count -gt 0)
{
foreach ($banner in $banners.value)
{
$messageId = ($banner | Get-Member -Type NoteProperty).Name
Write-Host "ID: $messageId"
Write-Host " Type: $($banner."$messageId".level)"
Write-Host " Message: $($banner."$messageId".message)"
}
}
}
<#
.SYNOPSIS
Remove-NotificationBanner
.DESCRIPTION
Deletes a notification banner from Azure DevOps.
.PARAMETER ServiceUri
Provide the URI of the Azure DevOps Services organization or Azure DevOps Server collection you want to work on.
.PARAMETER BannerId
Provide the ID of the notification banner that should bel deleted.
.PARAMETER Quiet
Specify this switch to suppress confirmation prompts.
.PARAMETER GetCredentials
Specify this switch if you need to use special credentials when connecting to your Azure DevOps Server.
.PARAMETER UsePAT
Specify this switch if you need to provide a Personal Access Token to connect your Azure DevOps Server/Services.
.PARAMETER MaxRestCallRetries
Provide the number of retries for failing REST calls. In general, you shouldn't need this option. However, when running in an
unstable environment (e.g., unreliable network connection), retries can help by automatically rerunning failing REST calls.
.PARAMETER TraceErrors
Specify this switch to enabled extended error tracing using netsh. If you combine this with the MaxRestCallRetries parameter,
only the last retry is traced. Otherwise, every request is trace, but traces for succeeding network calls are deleted for security reasons.
Note: You need to run the script as a local administrator when using this switch. Otherwise, network tracing is not allowed.
.EXAMPLE
.\Remove-NotificationBanner.ps1 -ServiceUri http://MyTfs:8080/tfs/DefaultCollection -Id c6bd264d-3fe7-4e11-9e2d-a4e8d86e5fec -GetCredentials
Deletes the notification banner with ID c6bd264d-3fe7-4e11-9e2d-a4e8d86e5fec in the DefaultCollection of your local Azure DevOps Server using Windows credentials.
.EXAMPLE
.\Remove-NotificationBanner.ps1 -ServiceUri https://dev.azure.com/myOrg -Id c6bd264d-3fe7-4e11-9e2d-a4e8d86e5fec -UsePAT
Deletes the notification banner with ID c6bd264d-3fe7-4e11-9e2d-a4e8d86e5fec in the Azure DevOps Services organization myOrg using a PAT for authentication.
#>
function Remove-NotificationBanner() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, HelpMessage="Enter the URI of the Azure DevOps Services organization or Azure DevOps Server collection you want to work on.")]
[ValidateNotNullOrEmpty()]
[string]$ServiceUri,
[Parameter(Mandatory=$true, HelpMessage="Provide the ID of the notification banner that should bel deleted.")]
[ValidateNotNullOrEmpty()]
[string]$BannerId,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch to suppress confirmation prompts.")]
[switch]$Quiet,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch if you need to use special credentials when connecting to your Azure DevOps Server.")]
[switch]$GetCredentials,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch if you need to provide a Personal Access Token to connect your Azure DevOps Server/Services.")]
[switch]$UsePAT,
[Parameter(Mandatory=$false, HelpMessage="Provide the number of retries for failing REST calls. In general, you shouldn't need this option.")]
[int]$MaxRestCallRetries = 1,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch to enable extended error tracing using netsh.")]
[switch]$TraceErrors
)
Assert-Preconditions -command Remove-NotificationBanner
if (!(Get-Consent -message "Do you want to delete notification ID $($BannerId)?"))
{
Write-Host "Aborted."
exit 0
}
$apiVersion = Get-ApiVersion
$uri = "$ServiceUri/_apis/settings/entries/host/GlobalMessageBanners/$($BannerId)?$apiVersion"
Invoke-RestDelete -uri $uri
Write-Host "Notification banner deleted."
}
<#
.SYNOPSIS
Clear-NotificationBanners
.DESCRIPTION
Deletes all notification banners from Azure DevOps.
.PARAMETER ServiceUri
Provide the URI of the Azure DevOps Services organization or Azure DevOps Server collection you want to work on.
.PARAMETER Quiet
Specify this switch to suppress confirmation prompts.
.PARAMETER GetCredentials
Specify this switch if you need to use special credentials when connecting to your Azure DevOps Server.
.PARAMETER UsePAT
Specify this switch if you need to provide a Personal Access Token to connect your Azure DevOps Server/Services.
.PARAMETER MaxRestCallRetries
Provide the number of retries for failing REST calls. In general, you shouldn't need this option. However, when running in an
unstable environment (e.g., unreliable network connection), retries can help by automatically rerunning failing REST calls.
.PARAMETER TraceErrors
Specify this switch to enabled extended error tracing using netsh. If you combine this with the MaxRestCallRetries parameter,
only the last retry is traced. Otherwise, every request is trace, but traces for succeeding network calls are deleted for security reasons.
Note: You need to run the script as a local administrator when using this switch. Otherwise, network tracing is not allowed.
.EXAMPLE
.\Clear-NotificationBanners.ps1 -ServiceUri http://MyTfs:8080/tfs/DefaultCollection -GetCredentials
Deletes all notification banners in the DefaultCollection of your local Azure DevOps Server using Windows credentials.
.EXAMPLE
.\Clear-NotificationBanners.ps1 -ServiceUri https://dev.azure.com/myOrg -Id c6bd264d-3fe7-4e11-9e2d-a4e8d86e5fec -Quiet -UsePAT
Deletes all notification banners in the Azure DevOps Services organization myOrg without confirmation using a PAT for authentication.
#>
function Clear-NotificationBanners() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, HelpMessage="Enter the URI of the Azure DevOps Services organization or Azure DevOps Server collection you want to work on.")]
[ValidateNotNullOrEmpty()]
[string]$ServiceUri,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch to suppress confirmation prompts.")]
[switch]$Quiet,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch if you need to use special credentials when connecting to your Azure DevOps Server.")]
[switch]$GetCredentials,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch if you need to provide a Personal Access Token to connect your Azure DevOps Server/Services.")]
[switch]$UsePAT,
[Parameter(Mandatory=$false, HelpMessage="Provide the number of retries for failing REST calls. In general, you shouldn't need this option.")]
[int]$MaxRestCallRetries = 1,
[Parameter(Mandatory=$false, HelpMessage="Specify this switch to enable extended error tracing using netsh.")]
[switch]$TraceErrors
)
Assert-Preconditions -command Clear-NotificationBanners
if (!(Get-Consent -message "Are you sure you want to delete all notification banners?"))
{
Write-Host "Aborted."
exit 0
}
$apiVersion = Get-ApiVersion
$uri = "$ServiceUri/_apis/settings/entries/host/GlobalMessageBanners?$apiVersion"
Invoke-RestDelete -uri $uri
Write-Host "Notification banners deleted."
}
Export-ModuleMember -Function New-NotificationBanner, Get-NotificationBanners, Remove-NotificationBanner, Clear-NotificationBanners