-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-TextMessage.ps1
90 lines (75 loc) · 6.56 KB
/
Get-TextMessage.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
function Get-TextMessage
{
<#
.Synopsis
Gets text messages
.Description
Get text messages sent to a Twilio number
.Example
Get-TextMessage
.Link
Twilio.com
.Link
Send-TextMessage
#>
[OutputType([PSObject])]
param(
# The credential used to get the texts
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Management.Automation.PSCredential]
$Credential,
# A setting storing the credential
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string[]]
$Setting = @("TwilioAccountKey", "TwilioAccountSecret")
)
process {
if (-not $Credential -and $Setting) {
if ($setting.Count -eq 1) {
$userName = Get-WebConfigurationSetting -Setting "${Setting}_UserName"
$password = Get-WebConfigurationSetting -Setting "${Setting}_Password"
} elseif ($setting.Count -eq 2) {
$userName = Get-secureSetting -Name $Setting[0] -ValueOnly
$password= Get-secureSetting -Name $Setting[1] -ValueOnly
}
if ($userName -and $password) {
$password = ConvertTo-SecureString -AsPlainText -Force $password
$credential = New-Object Management.Automation.PSCredential $username, $password
} elseif ((Get-SecureSetting -Name "$Setting" -ValueOnly | Select-Object -First 1)) {
$credential = (Get-SecureSetting -Name "$Setting" -ValueOnly | Select-Object -First 1)
}
}
if (-not $Credential) {
Write-Error "No Twilio Credential provided. Use -Credential or Add-SecureSetting TwilioAccountDefault -Credential (Get-Credential) first"
return
}
#region Define the core parameters for Get-Web
$getWebParams = @{
WebCredential=$Credential
Url="https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/SMS/Messages.xml"
AsXml =$true
UseWebRequest = $true
}
#endregion Define the core parameters for Get-Web
#region Get each page full of text messages
do {
$twiResponse = Get-Web @getwebParams -Verbose |
Select-Object -ExpandProperty TwilioResponse
if (-not $twiResponse) { break }
if ($twiResponse.SmsMessages) {
$twiResponse.SmsMessages |
Select-Object -ExpandProperty SMSMessage |
ForEach-Object {
$_.pstypenames.clear()
$_.pstypenames.add('Twilio.TextMessage')
$_
}
}
if ($twiResponse.SmsMessages.NextPageUri) {
# If there was a next page URI, slightly change the URL we're requesting.
$getWebParams.Url="https://api.twilio.com" + $twiResponse.SmsMessages.NextPageUri
}
} while ($twiResponse.SmsMessages.NextPageUri)
#endregion Get each page full of text messages
}
}