Powershell: Create WebJob in Microsoft Azure

In The previous article we discussed about Automation: Powershell Script to create SQL Server in Microsoft Azure


<#
.SYNOPSIS
    Create Web App Job in Azure Environment

.DESCRIPTION

    This script creates a new Web app Job in the Azure Environment

.EXAMPLE

    .\CreateAzureWebSiteJob.ps1 -AzureSubscriptionName "dt - Dev" -WebSiteName "myPSTestWebSite" -WebSiteJobName "myPSTestWebSiteJob" -JobType "continuous" -JobFile "C:\test.bat"
#>
Powershell Script to Create WebJob in Microsoft Azure



[CmdletBinding(SupportsShouldProcess = $true)]

param(
    # The name of valide Azure Subscription associated with Account
    [Parameter(Mandatory = $true)]
    [string]$AzureSubscriptionName,

    # The webSite Name you want to create

    [Parameter(Mandatory = $true)]
    [string]$WebSiteName,

    # The webSite Job Name you want to create

    [Parameter(Mandatory = $true)]
    [string]$WebSiteJobName,

    # The webSite Job Type Name you want to create 'triggered' or 'continuous' Job

    [Parameter(Mandatory = $true)]
    [string]$JobType,

    # The full path to copy files from.
    [Parameter(Mandatory = $true)]
    [string]$JobFile
)

# The script has been tested on Powershell 3.0

Set-StrictMode -Version 3

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials


# To authenticate through Windows Azure Active Directory and downloads associated subscriptions.

#Add-AzureAccount

# Ensure the local path given exists. Create it if switch specified to do so.

if (-not (Test-Path $JobFile))
{
    throw "Source path '$LocalFilePath' does not exist.  Specify an existing valid path."
}

# To select specific subscription from available subscription "Visual Studio Enterprise with MSDN"

Select-AzureSubscription -SubscriptionName $AzureSubscriptionName

# Check if Windows Azure Powershell is avaiable

if ((Get-Module -ListAvailable Azure) -eq $null)
{
    throw "Windows Azure Powershell not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
}
else
{
    Write-Host "Windows Azure Powershell is available!!!"
}

# Create the website

$website = Get-AzureWebsite | Where-Object {$_.Name -eq $WebSiteName }
if (!($website -eq $null))
{
    Write-Host "Creating website Job for '$WebSiteName'."
New-AzureWebsiteJob -Name $WebSiteName -JobName $WebSiteJobName -JobType $JobType -JobFile $JobFile  
    Write-Host "Website Job '$WebSiteJobName' is created successfully."
}
else
{
    throw "WebJob creation operation Failed. Website is not available."
}

In the next Article we will discuss about Automation: Powershell Script  to Create Web App in Microsoft Azure

Labels: , , , ,