From 046965c369ded3014dcbf731f84324c467f5dd23 Mon Sep 17 00:00:00 2001
From: Emmanuel Ormancey <emmanuel.ormancey@cern.ch>
Date: Wed, 23 Feb 2022 12:15:36 +0100
Subject: [PATCH] Update powershell samples for targeted

---
 Powershell/send-notification/README.md           |  6 ++++--
 ...nd-notification-ps1 => send-notification.ps1} |  0
 .../send-targeted-notification.ps1               | 16 ++++++++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)
 rename Powershell/send-notification/{send-notification-ps1 => send-notification.ps1} (100%)
 create mode 100644 Powershell/send-notification/send-targeted-notification.ps1

diff --git a/Powershell/send-notification/README.md b/Powershell/send-notification/README.md
index ac94d2f..4e5829a 100644
--- a/Powershell/send-notification/README.md
+++ b/Powershell/send-notification/README.md
@@ -1,6 +1,8 @@
-# send-notification Powershell script
-
+# send-notification Powershell scripts
+- ```send-notification.ps1``` : sending a normal notification
+- ```send-targeted-notification.ps1``` : sending a targeted notification
 - Fill ```channelId``` and ```notifications_bearer``` from Notification service information
+- Tip: if content encoding is utf8, don't forget ```-encoding utf8``` parameter on ```get-content```  or equivalent.
 
 Use corresponding base uri depending on the target infrastructure:
 - https://notifications.web.cern.ch/api
diff --git a/Powershell/send-notification/send-notification-ps1 b/Powershell/send-notification/send-notification.ps1
similarity index 100%
rename from Powershell/send-notification/send-notification-ps1
rename to Powershell/send-notification/send-notification.ps1
diff --git a/Powershell/send-notification/send-targeted-notification.ps1 b/Powershell/send-notification/send-targeted-notification.ps1
new file mode 100644
index 0000000..9876a31
--- /dev/null
+++ b/Powershell/send-notification/send-targeted-notification.ps1
@@ -0,0 +1,16 @@
+# Target Notification channel
+$notifications_sendapi = "https://notifications.web.cern.ch/api/notifications"
+$notifications_bearer  = "......."
+$channelId = "......"
+
+[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
+
+# Targets array
+$targetUsers = [System.Collections.ArrayList]::new()
+$targetUsers.Add([pscustomobject]@{email = "john.doe@cern.ch" })
+
+$Notification = @{ target=$channelId; summary='Title'; priority='NORMAL'; body='description'; targetUsers=$targetUsers }
+$Payload = @{ notification=$Notification}
+$headers = @{Authorization = "Bearer $notifications_bearer"}
+# convertto-json needs -depth parameter in order to convert also the targetUsers array
+Invoke-RestMethod -Uri $notifications_sendapi -Method Post -ContentType 'application/json;charset=utf-8' -Headers $headers -Body (ConvertTo-Json $Payload -Depth 10)
\ No newline at end of file
-- 
GitLab