Click here to Skip to main content
12,891,744 members (54,657 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 1 Nov 2012

TIP: Create local users when deploying an Azure Web Role

, 1 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Configure an Azure solution to create users during deployment


Sometimes you want to create specific users when deploying/installing an Azure. This may be a bit tricky if you are not familiarized with IT topics. Here is a complete recipe to create a local group and local users.
Although I have tried this in an Azure solution, it may be helpful also for other kind of IIS-based platforms.

Required files

In order to run a script during installation, you need two files: a command file to launch the script, and the script file itself. In your Web Role Project, insert both files; let's name them setup.cmd and setup.ps1
It is very important to configure both them with the "Copy always" property in order to be included in the deployment package. Both files will be placed in the bin folder.

The first file is a batch file that will just launch the script with elevated permissions, so it will contain the following:

@echo off 
powershell -command "Set-ExecutionPolicy Unrestricted" 2>> err.out  
powershell .\setup.ps1 2>> err.out

Notice that if some error occurs, it will append the error message to a file called err.out You can add more information there, like the date/time of deployment.

The second file will do the real job. It defines a reusable function to create the users and attach them to a group; then it creates the group if it doesn't exists and finally invoke the user creation function for all the users needed.

# Function to create users and assign them to a group

function createuser($username, $password) 
   $user = $server.Create("User", $username)
   $user.psbase.invokeset("AccountDisabled", "False")

# Obtain a reference to the server object


# Create the group if it doesn't exist previously

$localGroups = $server.Children | where {$_.SchemaClassName -eq 'group'}  | % {$[0].ToString()} 

if ($localGroups -NotContains "MyGroup")
   $group = $server.Create("Group", "MyGroup")
   $group.description = "Group of users created automatically"

# Create the users if they don't exist previously

$localUsers = $server.Children | where {$_.SchemaClassName -eq 'user'}  | % {$[0].ToString()} 

if ($localUsers -NotContains "User1")   { createuser "User1"   "Password!One" }
if ($localUsers -NotContains "User2")   { createuser "User2"   "Password!Two" }

# etcetera

In order bring those files to life, you have to define a Task in your Azure's Cloud Project. Look for the file called ServiceDefinition.csdef and insert a Task tag into the WebRole section:

<ServiceDefinition xmlns="" schemaversion="2012-05.1.7" name="MyCloud">
  <WebRole name="YourServiceName" vmsize="Small">
    <Startup priority="1">
      <Task taskType="simple" executionContext="elevated" commandLine="setup.cmd" />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jaime Olivares
Peru Peru

Computer Electronics professional, Software Architect and senior Windows C++ and C# developer with experience in many other programming languages, platforms and application areas including communications, simulation systems, PACS/DICOM (radiology), GIS, 3D graphics and HTML5-based web applications.
Currently intensively working with Visual Studio and TFS.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 1 Nov 2012
Article Copyright 2012 by Jaime Olivares
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid