forked from Windos/BurntToast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample08_Default.ps1
53 lines (43 loc) · 1.29 KB
/
Example08_Default.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
#######################################################
# .Info
# Shows how to configure an (unsecured) http listener
# in the background to wait for and display as a toast
# a message received via an http call
#
#######################################################
$JobScript = {
try
{
Import-Module BurntToast
$Listener = [System.Net.HttpListener]::new()
$Listener.Prefixes.Add("http://localhost:9000/")
$Listener.Start()
While ($True)
{
$context = $Listener.GetContext()
$Reader = [System.IO.StreamReader]::new($context.Request.InputStream)
$ReadJson = $Reader.ReadToEnd()
if ($ReadJson -eq "Exit") {break}
$ReadObject = $ReadJson | ConvertFrom-Json
New-BurntToastNotification -Text $ReadObject.Text
$context.Response.Close()
}
}
catch
{
Write-Error $_
}
finally
{
$Listener.Stop()
$Listener.Prefixes.Clear()
$Listener.Close()
}
}
$ToastJob = Start-Job -Name ToastJob -ScriptBlock $JobScript
$Body = @{
Text = "Hello from API call!"
}
Invoke-RestMethod -Method Post -Body ($Body | ConvertTo-Json) -Uri "http://localhost:9000/"
# Causes shutdown of the listener
Invoke-RestMethod -Method Post -Body "Exit" -Uri "http://localhost:9000/"