PowerShell and Notify My Android (NMA)

Here is a PowerShell function which wraps around the Notify My Android notification API call. You can use it in your environment to Notify Android devices when you like.

Please note that line 57 below, that reads as


should actually be [ x m l ] $webpage (without spaces) so you will need to manually correct this (the limitations of the syntax highlighter plugin prevents this from displaying correctly).

function New-NMANotification
{
	<#
	.SYNOPSIS 
	This function takes supplied parameters to makes a call to the NotifyMyAndroid (NMA) service.
	.DESCRIPTION
	This function takes supplied parameters to makes a call to the NotifyMyAndroid (NMA)  service.
	The NotifyMyAndroid service will then push these notifications to the NMA application on your
	Android device. 
	.PARAMETER application
	The application that needs notification on.
	.PARAMETER event
	The type of event (e.g. Service down, Path lost).
	.PARAMETER description
	The full description of the event (10000 max).
	.PARAMETER priority
	A priority level for this notification. This is optional and in the future will be used to change the way NMA alerts you.
	.EXAMPLE
	New-NMANotification -priority 0 -application "esxihost-01" -event "Patching complete" -description "Server finished patching. Build version now 1157734"
	.EXAMPLES
	New-NMANotification -priority 2 -application "vcenter01" -event "DOWN: VMware VirtualCenter Management Webservices" -description "Detected as down at 10:27. Impact: Prevents users logging into the vSphere Client."
	.NOTES
	Author: Robin Malik
	#>
	
	
	# Leave previous two lines blank
	param(
		[Parameter(Mandatory=$true,HelpMessage="The application that needs notification on.")]
		[String]
		$application,
		
		[Parameter(Mandatory=$true,HelpMessage="The type of event (e.g. Service down, Path lost)")]
		[String]
		$event,
		
		[Parameter(Mandatory=$true,HelpMessage="The full description of the event (10000 max).")]
		[String]
		$description,

		[Parameter(HelpMessage="A priority level for this notification. This is optional and in the future will be used to change the way NMA alerts you.")]
		[ValidateSet("-2","-1","0","1","2")]
		[String]
		$priority = "0"
	)
	
	$nmaURL = "https://www.notifymyandroid.com/publicapi/notify"	
	$apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

	$parameters = "?apikey=$apikey&amp;amp;amp;application=$application&amp;amp;amp;event=$event&amp;amp;amp;description=$description&amp;amp;amp;priority=$priority"
	$fullurl = $nmaURL + $parameters	
	
	
    try
    {
        $webclient = new-object System.Net.WebClient
        $webpage = $webclient.DownloadString($fullurl)
        if($webpage.nma.success.code -eq 200)
        {
            return 1
        }
        else
        {
            return 0
        }
    }
    catch
    {
        Write-Debug $Error[0].Exception.Message
        return 0
    }

}

Example:
$result = New-NMANotification -priority 0 -application $application -event $event -description $description
if($result -eq 1)
{
    # Success
}
elseif($result -eq 0)
{
    # Failure
}
else
{
    # Unhandled
}

Leave a Reply

Your email address will not be published. Required fields are marked *