PushBullet.ps1

cls
function Get-PushbulletME
{
[CmdletBinding()]
    [OutputType([System.Management.Automation.PSCustomObject])]
    param(
        [Parameter(ValueFromPipeline=$true)]
        [System.String]
        $AccessToken,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,HelpMessage="192.168.1.2:9480" )]
        [System.String]
        $ProxyUrl
        )
        
                
    $headers = @{}
    $headers["Access-Token"] = "$AccessToken"
    $headers["Content-Type"] = "application/json"

    $meUrl = "https://api.pushbullet.com/v2/users/me"
    if($PSBoundParameters.ContainsKey("ProxyUrl"))
    {
        $pushbulletMe = Invoke-WebRequest -Uri $meUrl -Headers $headers -ContentType "application/json" -Proxy "http://$ProxyUrl" | select -ExpandProperty Content | ConvertFrom-Json
    }
    else
    {
        $pushbulletMe = Invoke-WebRequest -Uri $meUrl -Headers $headers -ContentType "application/json" | select -ExpandProperty Content | ConvertFrom-Json
    }
    return $pushbulletMe
}
function Get-PushbulletDevices
{
    [CmdletBinding()]
    [OutputType([System.Array])]
    param(
        [Parameter(ValueFromPipeline=$true)]
        [System.String]
        $AccessToken,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,HelpMessage="192.168.1.2:9480" )]
        [System.String]
        $ProxyUrl
    )
    
    $url = "https://api.pushbullet.com/v2/devices"
    $proxy = [System.Net.WebRequest]::GetSystemWebProxy()
    $proxy = new-object System.Net.WebProxy "$ProxyUrl"
    $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

    $request = New-Object System.Net.WebCLient
    $request.Headers.Add("Access-Token",$AccessToken)
    
    if($PSBoundParameters.ContainsKey("ProxyUrl"))
    {
        $request.Proxy = $proxy
    }
    
      
     
    $request.UseDefaultCredentials = $true ## Proxy credentials only
     
    $request.Proxy.Credentials = $request.Credentials
     
    #$request.DownloadFile($url, $out)
    $devices = $request.DownloadString($url) | ConvertFrom-Json | select -ExpandProperty devices   
    
    
    return $devices
}
function Send-PushbulletSMS
{
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        [System.String]
        $AccessToken,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,HelpMessage="192.168.1.2:9480" )]
        [System.String]
        $ProxyUrl,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName, Mandatory=$true,HelpMessage="iden from Get-PushbulletME" )]
        [System.String]
        $source_user_iden,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName, Mandatory=$true,HelpMessage="iden from Get-PushbulletDevices" )]
        [System.String]
        $target_device_iden,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName, Mandatory=$true,HelpMessage="The number of the person how gets the SMS" )]
        [System.String]
        $conversation_iden,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,HelpMessage="The SMS message" )]
        $message
        
    )
    
    $json = '{
      "push": {
        "conversation_iden": "+1 303 555 1212",
        "message": "Hello!",
        "package_name": "com.pushbullet.android",
        "source_user_iden": "ujpah72o0",
        "target_device_iden": "ujpah72o0sjAoRtnM0jc",
        "type": "messaging_extension_reply"
      },
      "type": "push"
    }'


    $jsonObj =  $json | ConvertFrom-Json

    #user_iden from https://api.pushbullet.com/v2/users/me
    $jsonObj.push.source_user_iden = $source_user_iden

    #device_iden from Get-PushbulletDevices
    $jsonObj.push.target_device_iden = $target_device_iden

    #Phone No to send SMS to
    $jsonObj.push.conversation_iden = $conversation_iden

    #MSG
    $jsonObj.push.message = $message

    Write-Verbose "$jsonObj | ConvertTo-Json"
    
    $url = "https://api.pushbullet.com/v2/ephemerals"
    $headers = @{}
    $headers["Access-Token"] = "$AccessToken"
    $headers["Content-Type"] = "application/json"

    if($PSBoundParameters.ContainsKey("ProxyUrl"))
    {
        $Request = Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body ($jsonObj | ConvertTo-Json) -ContentType "application/json" -Proxy "http://$ProxyUrl"
    }
    else
    {
        $Request = Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body ($jsonObj | ConvertTo-Json) -ContentType "application/json" 
    }
    if($Request.StatusCode -eq 200)
    {
        Write-Verbose "Sms: [ $message ] to [ $conversation_iden ] has send"
    }
    else
    {
        Write-Verbose "!!! Sms: $message to $conversation_iden does not send !!!" 
    }

}
function Send-PushbulletPush
{
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        [System.String]
        $AccessToken,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,HelpMessage="192.168.1.2:9480" )]
        [System.String]
        $ProxyUrl,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName, Mandatory=$true,HelpMessage="The Title of the notification" )]
        [System.String]
        $title,
        
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,Mandatory=$true,HelpMessage="The notification message" )]
        $message
        
    )
    
    $headers = @{}
    $headers["Access-Token"] = "$AccessToken"
    $headers["Content-Type"] = "application/json"
    $PushURL = "https://api.pushbullet.com/v2/pushes"
    
    $body = @{
                type = "note"
                title = $title
                body = $message
                }
    if($PSBoundParameters.ContainsKey("ProxyUrl"))
    {
        $Sendattempt = Invoke-WebRequest -Uri $PushURL -Headers $headers -Method Post -Body ($body | ConvertTo-Json) -Proxy "http://$ProxyUrl" -ErrorAction SilentlyContinue
    }
    else
    {
        $Sendattempt = Invoke-WebRequest -Uri $PushURL -Headers $headers -Method Post -Body ($body | ConvertTo-Json) -ErrorAction SilentlyContinue
    }
    if ($Sendattempt.StatusCode -eq "200"){ Write-Verbose "Push: [ $msg ] OK" }
    else {Write-Warning "error encountered"  }
}
function Test-PushBullet
{
#$headers = @{}
#$headers["Access-Token"] = "o.hUk94v2KKwtLaShV8gsdnfOsKlsfMsmm"
#$headers["Content-Type"] = "application/json"
#$URL = "https://api.pushbullet.com/v2/pushes"
}

$default = @{
AccessToken = "o.hsmm"
ProxyUrl = "192.168.1.1:9480"
}

$PushbulletME = Get-PushbulletME @default
$PushbulletDevices = Get-PushbulletDevices @default | ? {$_.type -eq "android"}

$now = Get-Date

Send-PushbulletPush -message "MSG: $now" -title "Title $now"  @default
Send-PushbulletSMS -source_user_iden $PushbulletME.iden -target_device_iden $PushbulletDevices.iden -conversation_iden "972123456" -message "Test $now" -verbose @default