Create Azure VM from PowerShell with IIS Web Server auto installed

I saw someone asking on a forum about how to create Azure VM with Windows feature pre-installed, i thought it would be nice weekend exercise to see if i can get a Azure virtual machine created from PowerShell with IIS Web Server auto installed.

Now, by no means, i am saying that the PowerShell script given below is the best way to do it, there could be better, easier and correct way of doing it, but hey … this got my job done, so it should help you as well.

So in below PowerShell script the only thing that you have to change is, replace wherever you see XXXXXX with your cloud service name (the name that appears in Azure cloud service section), i know i can add a variable and make it easier for you, perhaps in next version of script.

so let’s see the script.


#Let's select the VMImage first that we want to use to build Server
$getvmimage = "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201412.01-en.us-127GB.vhd"

# keep some variable handy which we will reuse many times
$user = "webadmin"
$pass = "P@ssw0rd"
$name = "Web60" #####This will be the name of your VM, you can changet if you wish to########

# Create new AzureVMconfig for Web60 and provide it as input to Azureprovisioningconfig and eventually to New-AzureVM
$newvm = New-AzureVMConfig -Name $name -InstanceSize Medium -ImageName $getvmimage | Add-AzureProvisioningConfig -Windows -AdminUsername $user -Password $pass
New-AzureVM -ServiceName XXXXXXXXXXX -VMs $newvm -WaitForBoot -Verbose

#Some Port & Session Variables
$port = Get-AzureEndpoint -Name PowerShell -VM (Get-AzureVM -ServiceName XXXXXXXXXXX -Name $name) | Select -ExpandProperty Port

Write-Host "$port"   ####No real importance, writing it on console only for testing##############

$credential = Get-Credential -Credential $user -Verbose
$sessionoption = New-PSSessionOption -SkipCACheck
$session =  New-PSSession -ComputerName XXXXXXXXX.cloudapp.net -Credential $credential -Port $port -UseSSL -SessionOption $sessionoption -Verbose

$getstatus = Get-AzureVM -ServiceName XXXXXXXXXX -name $name | select InstanceStatus, PowerState -Verbose
if ($getstatus -match "ReadyRole" -and "Started") {Write-Host "Server is Ready, Connecting Now"; Enter-PSSession -Session $session}
else {Write-Host "Unable to Connect, Something Wrong, Try Again"; $getstatus }

#Once PSSession  starts then it should install Webserver Role in remote server
#Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools -Verbose

Invoke-Command -Session $session  -ScriptBlock {Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools -Verbose} -Verbose

There are many more stuff that you can do it from this script by making some easy changes, like instead of Web-server you can deploy any other Windows Feature by just making some changes, or you can leave the windows feature about completely and just use the PowerShell remoting to login to remote server and do whatever you want to.

If you come across any bug, or if you know about a better way of doing it, just share in comment.

Till then happy learning.

Hardeep