Web Platform Installer in Windows Azure Startup Tasks

Lately I got the task to run some Azure cmdlets on a Worker Role virtual machine.

I succeeded :)

I’ve been asking Mr. Google for sure, but without getting a full picture, so I thought its going to be a good reason for my first meaningful blog entry.

For this I did the following steps:

  1. Create a Startup Task which runs my installs for the Worker Role’s virtual machineThe configuration of the startup task looks like in the ServiceDefinition.csdef. Note that in the following example the cmd file path is relative to the Worker Role’s csproj file. Also important that I’m passing a parameter EMULATED to the command file so that it doesn’t run always locally.
       <WorkerRole name="Worker" vmsize="Small">
        <Startup>
          <Task commandLine="StartupTasks\InstallAzurePowerShell.cmd" executionContext="elevated" taskType="simple" >
            <Environment>
              <Variable name="EMULATED">
                <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
              </Variable>
            </Environment>
          </Task>
        </Startup>
      </WorkerRole>
  2. Install Web Platform Installer on the Worker Role I downloaded the file wpilauncher.exe from the Microsoft site.
    If you install that, you can get the portable executable out of its install folder (in my case: C:\Program Files\Microsoft\Web Platform Installer) as described in the Channel 9 video.
    So I copied them (WebpiCmd.exe, Microsoft.Web.PlatformInstaller.dll) also to the StartupTasks project folder of my Worker project.
  3. Install the Product WindowsAzurePowershell with the command line tool of the Web Platform Installer. I created the InstallAzurePowerShell.cmd file in the visual studio project for the worker role. There is a nice story about this point at the end of the post, which might be funny for some :). So the content of the InstallAzurePowerShell.cmd file is as follows, the most of the below code is a copy from Cory, just wanted to make the scenario fully understandable:
    if "%EMULATED%"=="true" goto :EOF
    md "%~dp0appdata"
    cd "%~dp0appdata"
    cd..
    reg add "hku\.default\software\microsoft\windows\currentversion\explorer\user shell folders" /v "Local AppData" /t REG_EXPAND_SZ /d "%~dp0appdata" /f
    %~dp0\WebpiCmd.exe /Install /IISExpress /Products:WindowsAzurePowershell /accepteula /log:%~dp0WindowsAzurePowershell.log
    reg add "hku\.default\software\microsoft\windows\currentversion\explorer\user shell folders" /v "Local AppData" /t REG_EXPAND_SZ /d %%USERPROFILE%%\AppData\Local /f
    exit /b 0
    :EOF
  4. All the files you are going to include in your projects StartupTasks folder has to have the CopyAlways set on it.

Tiny little point:
There is a tiny-little problem while trying to run the Web Platform Installer. It tries to log into the AppData, which is NOT allowed for the “nt authority\system”. You are going to get errors like “Error opening installation log file. Verify that the specified log file location exists and is writable.” The solution I provided above, has a HACK workaround for this issue by changing the registry entry for the AppData. I have been using the idea found in the blog SyntaxC4, where it was just used without pointing out its importance. Thanks a lot! Really! In the further part, I’m telling the story of searching for a solution of this issue – only if you are interested :).


I was stuck
on the problem, because the webpicmd.exe gives me “Error opening installation log file. Verify that the specified log file location exists and is writable.” error messages, which are not coming if I’m running it with the azure remote user.

I checked with the whoami from the cmd that the “nt authority\system” is running the command file.

I’ve been trying the PsExec approach also without success.

I was digging in with the Reflector… also without success…

And then I came to the solution described above (in section 3.).

Advertisements

About Tamas Nemeth

Husband and proud father of two daughters in Nürnberg. I'm working as a Senior Software Developer and an enthusiastic Clean-Coder. I spend most of my free time with my family (playing, hiking, etc...). I also play table-tennis and badminton sometimes...
This entry was posted in Technical Interest and tagged , , , , . Bookmark the permalink.

5 Responses to Web Platform Installer in Windows Azure Startup Tasks

  1. tks says:

    Thanks for sharing your experience Tamas!
    Great job done.

  2. Pingback: End of Month Research Roundup – January 2014 | endjin blog

  3. eguerints says:

    Wow, spent a long time on the AppData issue, but from within a chef-client run. Your solution really helped!

  4. Arindam Basu says:

    you can also you a scheduled task of windows (create and execute for one time) , essentially that will create a local system account. That way, you may not need to deal with the registry modification.

  5. Jinfu Lin says:

    Great job! You just saved my day!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s