With the release of the Windows Azure SDK 1.3, Microsoft introduced Startup tasks to Windows Azure Service Definition files.
Startup Tasks XML Schema
Start-up tasks are configured using XML in the Cloud Service Definition file (.csdef) using the Startup and Task Element. Here is the Startup Element for my particular Cloud Service Definition file.
<Task commandLine="PowerShellScriptLauncher.cmd" executionContext="elevated" taskType="simple" />
Anatomy of the PowerShell Script Launcher
The best implementation of Startup Tasks would be to create a library of common tasks that are reusable. You can pass arguments into a PowerShell Script to provide specific details required for an individual deployment.
Enabling PowerShell Execution in Windows Azure
PowerShell is installed by default on Windows Azure, however ExecutionPolicy does not allow unsigned scripts to be run by default. In order for our PowerShell scripts to be executed it is necessary to change the ExecutionPolicy to allow RemoteSigned Scripts.
Windows Azure has two different Versions of Windows (Server 2008 SP1 & Server 2008 R2) which can be controlled by setting the osFamily and osVersion.
Changing PowerShell ExecutionPolicy on Server 2008 SP1
Server 2008 SP1 is considered osFamily 1 on Windows Azure.
PowerShell 1.0 didn’t support the Set-ExecutionPolicy command [which was introduced in PowerShell 2.0]. In order to get around this I’ve created a registry file which makes the ExecutionPolicy change in the Registry. Create a file “ExecutionPolicy.reg” and include:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds Microsoft.PowerShell] "ExecutionPolicy"="RemoteSigned"
Changing PowerShell ExecutionPolicy on Server 2008 R2
Server 2008 R2 is considered osFamily 2 on Windows Azure.
Server 2008 R2 is running PowerShell 2.0, so executing a Set-ExecutionPolicy command is possible. Create a file “ExecutionPolicy.ps1” and include:
Set-ExecutionPolicy RemoteSigned -Force
Now that you have created a way of executing PowerShell on Windows Azure you need to add the appropriate method to the Startup Task Script. Add the appropriate line below to your PowerShellLauncher.cmd file:
REM This is for Server 2008 SP1
REM This is for Server 2008 R2
Now you can execute full featured PowerShell commands to get your instances up to a Running State.