PSInstapush.psm1

Function Get-InstapushApp {
    <#
    .SYNOPSIS
    Gets a list of a user's Instapush applications
 
    .DESCRIPTION
    Get-InstapushApp gets a list of Instapush applications belongning to the user specified by the UserToken parameter.
    .PARAMETER UserToken
    The token of the user to retrieve Instapush applications for.
 
    .INPUTS
    System.String
 
 
    You can pipe String objects to the UserToken parameter of Get-InstapushApp
 
    .OUTPUTS
    System.Management.Automation.PSCustomObject
 
 
    Get-InstapushApp returns a PSCustomObject with Instapush's response.
 
    .EXAMPLE
    PS C:\> Get-InstapushApp -UserToken <token string>
 
    This command will retrieve the list of applications belonging to the user token specified
     
    .LINK
    https://instapush.im/developer/rest
    #>


    #Requires -Version 3.0
    [CmdletBinding(HelpURI='https://gallery.technet.microsoft.com/Send-Notifications-to-1079573c')]

    Param(
        [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
            [String[]]$UserToken
    )

    Process {
        foreach ($ut in $UserToken) {
            $header = @{"x-instapush-token"=$ut}

            Write-Verbose "Sending data to Instapush"
            try {
                #Send to Instapush and return the response if successful
                $response = Invoke-RestMethod -Headers $header -Uri https://api.instapush.im/v1/apps/list -Method Get -ea Stop

                Write-Verbose "Data sent through Instapush successfully"
                if (-not $response) {
                    Write-Error "There was no response from Instapush. There are no appplications associated with the user token $ut."
                }
                else { $response }
            
            }
            catch { 
                #Generate error, but also build response so that error details from Instapush are exposed
                if (-not $_.ErrorDetails.Message) {
                    Write-Error "An error occurred while sending data to Instapush: $($_.Exception.Message)"
                }
                else {
                    $response = $_.ErrorDetails.Message | ConvertFrom-Json
                    Write-Error "An error occurred while sending data to Instapush: $($response.msg)"
                }
            }
        }
    }
}

Function New-InstapushApp {
    <#
    .SYNOPSIS
    Creates a new Instapush application.
 
    .DESCRIPTION
    New-InstapushApp creates a new Instapush application with a name specified by the Name parameter under the user specified in the UserToken parameter.
    .PARAMETER UserToken
    The token of the user to retrieve Instapush applications for.
 
    .PARAMETER Name
    The name to assign to the new application.
 
    .INPUTS
    System.String
 
 
    You can pipe String objects to the Name parameter of New-InstapushApp
 
    .OUTPUTS
    System.Management.Automation.PSCustomObject
 
 
    New-InstapushApp returns a PSCustomObject with Instapush's response.
 
    .EXAMPLE
    PS C:\> New-InstapushApp -UserToken <token string> -Name "Test Application"
 
    This command will create a new Instapush application titled "Test Application" under the user given to the UserToken parameter.
     
    .LINK
    https://instapush.im/developer/rest
    #>


    #Requires -Version 3.0
    [CmdletBinding(HelpURI='https://gallery.technet.microsoft.com/Send-Notifications-to-1079573c')]

    Param(
        [Parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$UserToken,

        [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
            [String[]]$Name
    )

    Process {
        foreach ($n in $Name) {
            $header = @{"x-instapush-token"=$UserToken}
            $body = @{title=$n} | ConvertTo-Json

            Write-Verbose "Sending data to Instapush"
            try {
                #Send to Instapush and return the response if successful
                $response = Invoke-RestMethod -Headers $header -Body $body -Uri https://api.instapush.im/v1/apps/add -Method Post -ea Stop

                Write-Verbose "Data sent through Instapush successfully"
                $response
            
            }
            catch { 
                #Generate error, but also build response so that error details from Instapush are exposed
                if (-not $_.ErrorDetails.Message) {
                    Write-Error "An error occurred while sending data to Instapush: $($_.Exception.Message)"
                }
                else {
                    $response = $_.ErrorDetails.Message | ConvertFrom-Json
                    Write-Error "An error occurred while sending data to Instapush: $($response.msg)"
                }
            }
        }
    }
}

Function Get-InstapushEvent {
    <#
    .SYNOPSIS
    Gets a list of events associated with an Instapush application.
 
    .DESCRIPTION
    Get-InstapushEvent retrieves the list of events that are associated with an Instapush application that is specified by sending the application id and application secret to the AppId and AppSecret parameters respectively. Both are required to complete the request.
    .PARAMETER AppId
    The ID of the application to get the events of.
 
    .PARAMETER AppSecret
    The secret key of the application to get the events of.
 
    .INPUTS
    System.Management.Automation.PSCustomObject
 
 
    You can pipe PSCustomObjects objects to Get-InstapushEvent that have properties matching the parameter names.
 
    .OUTPUTS
    System.Management.Automation.PSCustomObject
 
 
    Get-InstapushEvent returns a PSCustomObject with Instapush's response.
 
    .EXAMPLE
    PS C:\> Get-InstapushEvent -AppId <application ID string> -AppSecret <application secret key string>
 
    This command will get the events for the application specified by the ID and key strings. Note that both are required.
    .EXAMPLE
    PS C:\> Get-InstapushApp -UserToken <token string> | Get-InstapushEvent
     
    This command uses Get-InstapushApp to retrieve a user's applications, and then pipes the application response to Get-InstapushEvent to retrieve the applications' events.
    .LINK
    https://instapush.im/developer/rest
    #>


    #Requires -Version 3.0
    [CmdletBinding(HelpURI='https://gallery.technet.microsoft.com/Send-Notifications-to-1079573c')]

    Param(
        [Parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$AppId,

        [Parameter(Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$AppSecret
    )

    Process {
        $header = @{"x-instapush-appid"=$AppId
                    "x-instapush-appsecret"=$AppSecret}

        Write-Verbose "Sending data to Instapush"
        try {
            #Send to Instapush and return the response if successful
            $response = Invoke-RestMethod -Headers $header -Uri https://api.instapush.im/v1/events/list -Method Get -ea Stop

            Write-Verbose "Data sent through Instapush successfully"
            $response
            
        }
        catch { 
            #Generate error, but also build response so that error details from Instapush are exposed
            if (-not $_.ErrorDetails.Message) {
                Write-Error "An error occurred while sending data to Instapush: $($_.Exception.Message)"
            }
            else {
                $response = $_.ErrorDetails.Message | ConvertFrom-Json
                Write-Error "An error occurred while sending data to Instapush: $($response.msg)"
            }
        }
    }
}

Function New-InstapushEvent {
    <#
    .SYNOPSIS
    Creates a new Instapush application event.
 
    .DESCRIPTION
    New-InstapushEvent creates a new event on the application given to the AppId and AppSecret parameters, and both are required. The title of the event is supplied to the Name parameter. The Trackers parameter requires an array of parameters to use in the event, and the Message parameter is the message pattern.
 
    The trackers are referred to in the message using braces, {}, so if the trackers are given as @(value1, value2, value3), then the message would contain a string that references each one at least one: "The {value1} is not {value2}, but it is {value3}." All trakcers supplied must be present in the message string, and they are case sensitive.
    .PARAMETER AppId
    The ID of the application to add a new event to.
 
    .PARAMETER AppSecret
    The secret key of the application to add a new event to.
 
    .PARAMETER Name
    The name to be given to the event.
 
    .PARAMETER Trackers
    The array of parameters that will be used within the event message.
 
    .PARAMETER Message
    The message that will be sent when the event is triggered.
 
    .INPUTS
    System.Management.Automation.PSCustomObject
 
 
    You can pipe PSCustomObjects objects to New-InstapushEvent that have properties matching the parameter names.
 
    .OUTPUTS
    System.Management.Automation.PSCustomObject
 
 
    New-InstapushEvent returns a PSCustomObject with Instapush's response.
 
    .EXAMPLE
    PS C:\> New-InstapushEvent -AppId <application ID string> -AppSecret <application secret key string> -Name TestEvent -Trackers @(Name, Country) -Message "{Name} is from the country {Country}."
 
    This command will create an event labeled TestEvent that has parameters of Name and Country.
    .EXAMPLE
    PS C:\> Get-InstapushApp -UserToken <token string> | New-InstapushEvent -Name AnotherEvent -Trackers Name, Value -Message "{Name} has {Value}"
     
    This command uses Get-InstapushApp to retrieve a user's applications, and then pipes the application response to Get-InstapushEvent create a new event on each application under the user's account.
    .LINK
    https://instapush.im/developer/rest
    #>


    #Requires -Version 3.0
    [CmdletBinding(HelpURI='https://gallery.technet.microsoft.com/Send-Notifications-to-1079573c')]

    Param(
        [Parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$AppId,

        [Parameter(Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$AppSecret,

        [Parameter(Position=2,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$Name,

        [Parameter(Position=3,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String[]]$Trackers,

        [Parameter(Position=4,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$Message
    )

    Process {
        $header = @{"x-instapush-appid"=$AppId
                    "x-instapush-appsecret"=$AppSecret}

        $body = @{title=$Name
                  trackers=$Trackers
                  message=$Message} | ConvertTo-Json

        Write-Verbose "Sending data to Instapush"
        try {
            #Send to Instapush and return the response if successful
            $response = Invoke-RestMethod -Headers $header -Body $body -Uri https://api.instapush.im/v1/events/add -Method Post -ea Stop

            Write-Verbose "Data sent through Instapush successfully"
            $response
            
        }
        catch { 
            #Generate error, but also build response so that error details from Instapush are exposed
            if (-not $_.ErrorDetails.Message) {
                Write-Error "An error occurred while sending data to Instapush: $($_.Exception.Message)"
            }
            else {
                $response = $_.ErrorDetails.Message | ConvertFrom-Json
                Write-Error "An error occurred while sending data to Instapush: $($response.msg)"
            }
        }
    }
}

Function Send-Instapush {
    <#
    .SYNOPSIS
    Send an Instapush notification
 
    .DESCRIPTION
    Send-Instapush sends an Instpush notification to the devices that are attached to the user account that owns the application given to the AppId and AppSecret parameters.
 
    The event that is triggered is the title of the event that is supplied to the Name parameter. The Trackers parameter expects a hashtable of parameter names with their values to be interpolated into the message of the notification.
    .PARAMETER AppId
    The ID of the application to trigger an event on.
 
    .PARAMETER AppSecret
    The secret key of the application to trigger an event on.
 
    .PARAMETER Name
    The name of the event to be triggered.
 
    .PARAMETER Trackers
    A hashtable of parametes to send to the event.
 
    .INPUTS
    System.Management.Automation.PSCustomObject
 
 
    You can pipe PSCustomObjects objects to Send-Instapush that have properties matching the parameter names.
 
    .OUTPUTS
    System.Management.Automation.PSCustomObject
 
 
    Send-Instapush returns a PSCustomObject with Instapush's response.
 
    .EXAMPLE
    PS C:\> Send-Instapush -AppId <application ID string> -AppSecret <application secret key string> -Name TestEvent -Trackers @{Name='John Doe';Country='USA'
 
    This command will trigger the TestEvent on the Instapush application and will pass along the Name and Country parameter values to the event.
    .LINK
    https://instapush.im/developer/rest
    #>

    
    #Requires -Version 3.0
    [CmdletBinding(HelpURI='https://gallery.technet.microsoft.com/Send-Notifications-to-1079573c')]

    Param(
        [Parameter(Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$AppId,

        [Parameter(Position=2,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$AppSecret,

        [Parameter(Position=3,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [String]$Name,

        [Parameter(Position=4,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
            [Hashtable]$Trackers
    )

    Process {
        $header = @{"x-instapush-appid"=$AppId
                    "x-instapush-appsecret"=$AppSecret}

        $body = @{event=$Name
                  trackers=$Trackers} | ConvertTo-Json

        Write-Verbose "Sending data to Instapush"
        try {
            #Send to Instapush and return the response if successful
            $response = Invoke-RestMethod -Headers $header -Body $body -Uri https://api.instapush.im/v1/post -Method Post -ea Stop

            Write-Verbose "Data sent through Instapush successfully"
            $response
            
        }
        catch { 
            #Generate error, but also build response so that error details from Instapush are exposed
            if (-not $_.ErrorDetails.Message) {
                Write-Error "An error occurred while sending data to Instapush: $($_.Exception.Message)"
            }
            else {
                $response = $_.ErrorDetails.Message | ConvertFrom-Json
                Write-Error "An error occurred while sending data to Instapush: $($response.msg)"
            }
        }
    }
}

New-Alias -Name snip -Value Send-Instapush
New-Alias -Name gipa -Value Get-InstapushApp
New-Alias -Name nipa -Value New-InstapushApp
New-Alias -Name gipe -Value Get-InstapushEvent
New-Alias -Name nipe -Value New-InstapushEvent