Sunday, 20 November 2011

Executing PowerShell script with Windows Azure Startup Task


In previous Windows Azure Startup Tasks post, we have seen how to do jobs required when starting the role with Windows Azure. But, the Startup Task executes batch file (*.cmd) which actually contains sequence of DOS commands.

In this post, we will see how to execute PowerShell script when starting the role using Startup Task feature.

Step 1: Create an Azure Project with Web or Worker Role. (Note: Startup task is not supported in VM Role).

Step 2: Create a PowerShell script file (*.ps1) (using notepad or some other tool) and include necessary commands and Save the file into WebRole or WorkerRole directory created (I named the file to PowerShellTask.ps1).
md C:\PowerShellTest
cd C:\PowerShellTest
Get-Process >> ./GetProcess.txt
Get-Service >> ./GetService.txt
Get-EventLog -LogName System -Newest 100 >> GetEventLog.txt
For testing reason, I have some command for creating a folder called PowerShellTest under C drive and exporting the system logs using Get-Process, Get-Service and Event Logs to different files.

Step 3: Add the .ps1 file (PowerShellTask.ps1) into the WebRole or WorkerRole project which required to run the PowerShell command file when starting up and change the Copy to output Directory property to Copy always.


Step 4: Create a command line script file in the same Web/Worker Role project (with extention as .cmd) using any character based editor such as Notepad and Save the file.
if "%EMULATED%"=="true" goto :EOF
powershell -command "Set-ExecutionPolicy Unrestricted" 2>> error.out
powershell .\PowerShellTask.ps1 2>> error.out
The script file execute two PowerShell command
powershell -command "Set-ExecutionPolicy Unrestricted" 2>> error.out
By default PowerShell script will run with default execution policy as Restricted. But Power Script should run with no restriction if any installations are carried out with the scripts. So the above statement set the execution policy to unrestricted and logs the output if any to error.out.
powershell .\PowerShellTask.ps1 2>> error.out
This statement executes PowerShellTask.ps1 and log the output to error.out.

The following command skips the execution to the end if the role starts in development emulator. So when developing the application, this scripts will not run in the local system.
if "%EMULATED%"=="true" goto :EOF
The EMULATED is an environment variable will be configured in the definition file. The values are queried using xpath query from RoleEnvironment.

Step 5: Add the file into the project and change the Copy to output Directory property to Copy always.


Step 6: (Optional) if you do any change in the script file using Visual Studio after it's been added, the Visual Studio saves the file in byte code. But the azure Startup Task required the script file in Unicode (UTF-8 without signature) format. So open the cmd file and select File -> Advanced Save Options… menu. Select Unicode (UTF-8 without signature) option from the Advanced Save Options popup window and click OK and Save the file.



Step 7: To add Startup Task in to the Azure project, open the ServiceDefinition.csdef file and add <startup> node with requited <task> node.
For more information about commandLine, taskType and executionContext attributes and its values, please refer msdn and below pages.
http://msdn.microsoft.com/en-us/library/windowsazure/gg456327.aspx
http://www.dotnettwitter.com/2011/11/windows-azure-startup-tasks.html


Step 8: Deploy the application in Azure environment and see the output.


Note: This method works fine with Azure Guest OS v1.* and v2.* (Windows Server 2008 SP2, Windows Server R2).


1 Response to “Executing PowerShell script with Windows Azure Startup Task”

  • Anonymous says:
    25 October 2013 at 19:00

    Thanks *a lot* for the time-saver!

Post a Comment