Monday, November 14, 2011

Setting environment variables in Windows Azure

At the latest when you want to run java applications on Windows Azure you need to set several environment variables. Since there are startup tasks you may perform before your Role is being started, this obviously is the place where to set these variables. But there are some difficulties:

Setting environment variables Intentional-Style

Setting environment variables using the well known set command line tool works on Windows Azure like on premise:

set MYPATH=C:\Directory\

BUT:


  • This only works for one command shell session

  • This only works for the current user

The variables set should be available all time and for all users. So they need to be set as system variables. And system variables may only be set by an administrator.


Setting environment variables correctly



  1. Make sure your startup script is run with elevated permissions.
    Check your .csdef file to look similar to this:
    <Startup>
      <Task commandLine="Startup.cmd" 
            executionContext="elevated" 
            taskType="simple" />
    </Startup>

  2. Use the setx command line tool with the /M parameter to set system variables:
    setx MYPATH C:\Directory\ /M

  3. If you need the variables set to use them in your startup script, you need to set them with the set command again.
    - set works for the current session (the startup script)
    - setx
    works for all session started after setting the variables

References:

How to use the setx command: http://ss64.com/nt/setx.html

No comments:

Post a Comment