-
Notifications
You must be signed in to change notification settings - Fork 4
/
Send Push-Mesage.ps1
167 lines (132 loc) · 6.27 KB
/
Send Push-Mesage.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
Function Send-PushMessage {
[CmdletBinding(DefaultParameterSetName='Message')]
#Usage Examples:
#Send an Address: Send-PushMessage -Type Address -PlaceName "Bob's House" -PlaceAddress "5555 Ashburn Lake Dr Tampa Fl 33610"
#Send a Message: Send-PushMessage -Type Message -Title "This is a test" -msg "The message goes here!"
#Send a Link: Send-PushMessage -Type Link -Title "This Is a Link" -msg "Link Text Goes here!" -url "www.pushbullet.com"
#Send a File: Send-PushMessage -Type File -Filename "Webroot.zip" -FileType "Anything" -url "http://download.webroot.com/Webroot-Deploy-Solution.zip"
#Send a list: **This Function is not yet working** Send-PushMessage -Type List -title "My List" -items "Item 1, Item 2, Item 3"
#Upload a file: **This Function is not yet working**
param(
[Parameter(Mandatory=$false,ParameterSetName="File")]$FileName,
[Parameter(Mandatory=$true, ParameterSetName="File")]$FileType,
[Parameter(Mandatory=$true, ParameterSetName="File")]
[Parameter(Mandatory=$true, ParameterSetName="Link")]$url,
[Parameter(Mandatory=$false,ParameterSetName="Address")]$PlaceName,
[Parameter(Mandatory=$true, ParameterSetName="Address")]$PlaceAddress,
[Parameter(Mandatory=$false)]
[ValidateSet("Address","Message", "File", "List","Link")]
[Alias("Content")]
$Type,
[switch]$UploadFile,
[string[]]$items,
$title="PushBullet Message",
$msg)
begin{
$api = "xxxxxxxxxxxx" #Hard set the API key here.
$PushURL = "https://api.pushbullet.com/v2/pushes"
$devices = "https://api.pushbullet.com/v2/devices"
$uploadRequestURL = "https://api.pushbullet.com/v2/upload-request"
$uploads = "https://s3.amazonaws.com/pushbullet-uploads"
$cred = New-Object System.Management.Automation.PSCredential ($api,(ConvertTo-SecureString $api -AsPlainText -Force))
if (($PlaceName) -or ($PlaceAddress)){$type = "address"}
}
process{
switch($Type)
{
'Address'
{
$body = @{
type = "address"
title = $Placename
address = $PlaceAddress
}
}
'Message'
{
$body = @{
type = "note"
title = $title
body = $msg
}
}
'List'
{
$body = @{
type = "list"
title = $title
items = $items
}
"body preview"
$body
}
'Link'
{
$body = @{
type = "link"
title = $title
body = $msg
url = $url
}
}
'File'
{
If ($UploadFile)
{
$UploadRequest = @{
file_name = $FileName
fileType = $FileType
}
#Ref: Pushing files https://docs.pushbullet.com/v2/pushes/#pushing-files
# "Once the file has been uploaded, set the file_name, file_url, and file_type returned in the response to the upload request as the parameters for a new push with type=file."
#Create Upload request first
$attempt = Invoke-WebRequest -Uri $uploadRequestURL -Credential $cred -Method Post -Body $UploadRequest -ErrorAction SilentlyContinue
If ($attempt.StatusCode -eq "200")
{
Write-Verbose "Upload Request OK"
}
else
{
Write-Warning "error encountered, check `$Uploadattempt for more info"
$global:Uploadattempt = $attempt
}
#Have to include the data field from the full response in order to begin an upload
$UploadApproval = $attempt.Content | ConvertFrom-Json | select -ExpandProperty data
#Have to append the file data to the Upload request
$UploadApproval | Add-Member -Name "file" -MemberType NoteProperty -Value ([System.IO.File]::ReadAllBytes((get-item C:\TEMP\upload.txt).FullName))
#Upload the file and get back the url
#$UploadAttempt =
#Invoke-WebRequest -Uri $uploads -Credential $cred -Method Post -Body $UploadApproval -ErrorAction SilentlyContinue
#Doesn't work...maybe invoke-restMethod is the way to go?
Invoke-WebRequest -Uri $uploads -Method Post -Body $UploadApproval -ErrorAction SilentlyContinue
#End Of Upload File scriptblock
}
Else {
#If we don't need to upload the file
$body = @{
type = "file"
file_name = $fileName
file_type = $filetype
file_url = $url
body = $msg
}
}
$global:UploadApproval = $UploadApproval
BREAK
#End of File switch
}
}
write-debug "Test-value of `$body before it gets passed to Invoke-WebRequest"
$Sendattempt = Invoke-WebRequest -Uri $PushURL -Credential $cred -Method Post -Body $body -ErrorAction SilentlyContinue
If ($Sendattempt.StatusCode -eq "200")
{
Write-Verbose "OK"
}
else
{
Write-Warning "error encountered, check `$attempt for more info"
$global:Sendattempt = $Sendattempt
}
}
end{$global:Sendattempt = $Sendattempt}
}