A hosted service in Windows Azure consists of an application that is designed to run in one or more virtual machines in a data center managed by Microsoft.
Since the application is running remotely, you may obviously be interested in the health of the overall application to see if the application is running smoothly or has encountered an error because of which the application was suspended or even shutdown unexpectedly. One way to check the overall health of the application is by making use of Windows Azure Service Management API and writing some code which makes use of this API. Alternately you can use our product Azure Management Cmdlets which will do the work for you.
Here I am going to show you a simple script which sends alert every time status of your deployment changes using our Azure Management Cmdlets product. Please take a look at the PowerShell script below:
Sample Script
#######################################################################
#### Sample powershell script to get an email alert every time when
#### deployment status changes
#### Replace the parameters specified in <> with actual parameters.
#######################################################################
# Slot (Production or Staging)
$slot = "<name of the slot: Production or Staging>";
# Name of the hosted service
$serviceName = "<name of the hosted service>";
# Subscription Id
$subscriptionId = "<your subscription id>";
# Following example illustrates how to create a certificate object for a certificate present in certificate store.
$certificate = Get-ChildItem -path cert:\<certificate store name>\<certificate store location>\<certificate thumbprint>
# Refresh frequency i.e. time interval in seconds after which status of a hosted service is fetched (you may want to keep this value somewhat higher say 300 seconds i.e. 5 minutes)
$refreshFrequency = "<value of refresh frequency in seconds>";
# Email address from where you want to send email alert
$emailFrom = "<specify sender's email id>";
# Email address to which you want to send email alert
$emailTo = "<specify receiver's email id>";
# Email subject
$subject = "<specify subject of email>";
# Email body
$body = "<specify email body>";
# SMTP server name
$smtpServer = "<smtp server name>";
# User name of your email account
$userName = "<specify your user name>";
# Password of your email address
$password = "<specifies your password>";
# Credentials of your
$securePassword = convertto-securestring -string $password -asplaintext -force
$credential = New-Object System.Management.Automation.PSCredential($userName, $securePassword)
$space = " ";
# First get the status of the slot
$result = Get-Deployment -Slot $slot -ServiceName $serviceName -SubscriptionId $subscriptionId -Certificate $certificate
if($result)
{
for(;;)
{
# Save this status. This would be the initial status
$deploymentStatus = $result.DeploymentStatus;
# Now wait for some time
Sleep $refreshFrequency
# Again fetch the status
$result = Get-Deployment -Slot $slot -ServiceName $serviceName -SubscriptionId $subscriptionId -Certificate $certificate -Verbose
# Check if there is a change in status. If so, then send out the email
if($result -and !$result.DeploymentStatus.Equals($deploymentStatus))
{
$changedStatus = $result.DeploymentStatus;
Send-MailMessage -To $emailTO -From $emailFrom -Body $body$space"""$deploymentStatus$space"to"$space$changedStatus""" -SmtpServer $smtpServer -Subject $subject -Credential $credential -UseSsl
}
}
}
Brief Explanation of the Parameters:
1. -Body | Specifies the body (content) of the e-mail message. |
2. -Certificate | This is the API certificate. This must be uploaded already in Windows Azure Portal. |
3. -Credential | Specifies a user name and password that has permission to perform this action (For sending email). |
4. -From | Specifies the address from which the mail is to be sent. |
5. -To | Specifies the addresses to which the mail is to be sent. |
6. -Slot | “Production” or “Staging” slot for which information needs to be fetched. |
7. -SubscriptionId | Id of the subscription. You can find this information by visiting Windows Azure Portal at https://windows.azure.com |
8. -ServiceName | Name of the hosted service. |
9. -Subject | Specifies the subject of the e-mail message. This parameter is required. |
10. -SmtpServer | Specifies the name of the SMTP server that sends the e-mail message. |
11. -UseSsl | Uses the Secure Sockets Layer (SSL) protocol to establish a connection to the remote computer to send mail. By default, SSL is not used. |
How does this script work?
Once this cmdlet is executed, what it does is that it tries to fetch deployment properties once every “x” seconds (specified via $refreshFrequency variable). After fetching deployment properties it checks if current status of deployment is different from recently fetched deployment status if it is different than earlier one then an automatic email alert will be send to specified email id.
Summary
In this way, you can create your own scripts using Cerebrata’s Azure Management Cmdlets product at your convenience to ease your work. Do try it out and let us know how it has worked for you. You can download the latest version of Azure management Cmdlets from here: http://www.cerebrata.com/products/azure-management-cmdlets/introduction.