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:
- 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>
- 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.
- 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
- 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.).