-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_banners.ps1
More file actions
135 lines (107 loc) · 4.79 KB
/
Copy pathset_banners.ps1
File metadata and controls
135 lines (107 loc) · 4.79 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
# Use case: create Azure DevOps notification banners using Powershell, REST API and JSON files.
# The script is searching for files with the name schema banners*.json which must be located in the current working directory.
# Each json files contains one banner (message, loglevel, expire date).
# To start the script you need the collection / org url and a PAT.
# The PAT must be encoded as a secure string.
# e.g. In Powershell 7 you can create a secure string with "$pat = read-host -AsSecureString"
# Format of json files
#{
# "Message": "<string>",
# "Level":"Error/Warning/Info",
# "ExpirationDate":"2022-10-23T14:48:00.000Z",
# "Prio":"p0/p1/p2"
#}
# Inspired by https://github.com/microsoft/banner-settings-ado-extension
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$collectionUrl,
[Parameter(Mandatory=$true)]
[SecureString]$pat)
#Auth against AzD service or server
$patPlain = ConvertFrom-SecureString $pat -AsPlainText;
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("PAT:$($patPlain)"))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
$bannerFileNames = Get-ChildItem -Filter banner*.json
if ($bannerFileNames.Count -eq 0) {
write-host "Found no banner files"
exit 100
}
else {
Write-Host "Found $($bannerFileNames.Count) banner files." -ForegroundColor Blue
}
#retrieve all existing banners
$apiUri = "{0}/_apis/settings/entries/host/GlobalMessageBanners?api-version=6.0-preview" -f $collectionUrl
$response = Invoke-WebRequest -Uri $apiUri -Headers $Headers
if ($response.StatusCode -eq 200) {
$content = $response.Content | ConvertFrom-Json
$messageIds = [System.Collections.Generic.List[string]]::new()
$content.value | get-member | Where-Object { $_.MemberType -eq 'NoteProperty' } | ForEach-Object { $messageIDs.Add($_) }
write-host "New banners:" -ForegroundColor Green
$json = ($response.Content | ConvertFrom-Json).value
ForEach ($banner in $json.PSObject.Properties) {
Write-Host ("{0} : {1} : {2}" -f $banner.Value.message, $banner.Value.Level, $banner.Value.ExpirationDate)
}
}
# Delete all existing banners
$response = Invoke-WebRequest -Uri $apiUri -Headers $Headers -Method Delete
write-host "Deleted all existing banners" -ForegroundColor Yellow
# add new banner
$apiUri = "{0}/_apis/settings/entries/host?api-version=6.0-preview" -f $collectionUrl
#Sample
#{"GlobalMessageBanners/p2-1666102239075":{"level":"Info","message":"rest"}}
$firstFile = $true;
$bannerEntries = "{";
foreach ($filename in $bannerFileNames) {
[string]$banner = Get-Content $filename
[psobject]$banner = ConvertFrom-Json -InputObject $banner
$priority = $banner.Prio #p0, p1, p2
$level = $banner.Level; #Warning, Info, Error
$message = $banner.Message
$expirationDate = $banner.ExpirationDate; #e.g. 2011-10-05T14:48:00.000Z
$date = get-date
$pattern = "MM/dd/yyyy H:mm:ss"
if ([DateTime]::ParseExact($expirationDate,$pattern,$null) -gt $date) {
[string] $messageID = Get-Date | Select-Object Ticks -ExpandProperty Ticks
$messageID += get-random -Maximum 1000
$title = "GlobalMessageBanners/$priority-$messageID";
$entry = """$title"":{
""message"": ""$message"",
""level"": ""$level"",
""expirationDate"": ""$expirationDate""
}"
if (!$firstFile) {
$entry = ",$entry"
}
else {
$firstFile = $false;
}
$bannerEntries += $entry;
}
else {
write-host "Skipping banner file because expiration date is in the past"
}
}
$bannerEntries += "}";
$response = Invoke-WebRequest -Uri $apiUri -Headers $Headers -Method Patch -Body $bannerEntries -ContentType "application/json"
Write-Host "Adding new banners was successful" -ForegroundColor Yellow
#$addResponse
#retrieve all existing banners
$apiUri = "{0}/_apis/settings/entries/host/GlobalMessageBanners?api-version=6.0-preview" -f $collectionUrl
$response = $null
$content = $null
$messageIDs = $null
$response = Invoke-WebRequest -Uri $apiUri -Headers $Headers -Method Get
if ($response.StatusCode -eq 200) {
$content = $response.Content | ConvertFrom-Json
$messageIds = [System.Collections.Generic.List[string]]::new()
$content.value | get-member | Where-Object { $_.MemberType -eq 'NoteProperty' } | ForEach-Object { $messageIDs.Add($_) }
write-host "New banners:" -ForegroundColor Green
$json = ($response.Content | ConvertFrom-Json).value
ForEach ($banner in $json.PSObject.Properties) {
Write-Host ("{0} : {1} : {2}" -f $banner.Value.message, $banner.Value.Level, $banner.Value.ExpirationDate)
}
}