DEV Community

Wai Liu
Wai Liu

Posted on • Originally published at waiholiu.blogspot.com on

Install IIS on an Azure VM using PowerShell

This quick start article is useful (https://docs.microsoft.com/en-us/azure/virtual-machines/windows/quick-create-powershell) but if you just want a super quick way to get an Azure VM with a IIS web server up without having to remote into the server etc. and input anything into the terminal - just run the script below in a powershell terminal or on the Azure cloud shell.

Script

Part 1 - Set up credentials

$VMLocalAdminUser = "LocalAdminUser"

$VMLocalAdminSecurePassword = ConvertTo-SecureString "NotARealPassword!@#$%433" -AsPlainText -Force

$Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword);
Enter fullscreen mode Exit fullscreen mode

Part 2 - create RG and VM

New-AzResourceGroup -Name todeleteRG -Location EastUS
New-AzVm `
    -ResourceGroupName "todeleteRG" `
    -Name "todeleteVM" `
    -Location "East US" `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNetworkSecurityGroup" `
    -PublicIpAddressName "myPublicIpAddress" `
    -Credential $Credential `
    -OpenPorts 80, 443
Enter fullscreen mode Exit fullscreen mode

Part 3 - Install IIS and add default page with computer name

Set-AzVMExtension -ResourceGroupName "todeleteRG"`
    -ExtensionName "IIS" `
    -VMName "todeleteVM" `
    -Location "East US" `
    -Publisher Microsoft.Compute `
    -ExtensionType CustomScriptExtension `
    -TypeHandlerVersion 1.8 `
    -SettingString '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'
Enter fullscreen mode Exit fullscreen mode

Explanation

Part 1 will create a credential object with user name and password

Part 2 will actually create the VM.

Part 3 installs IIS with a dummy page that just displays the computername.

Verify it works.

If you navigate to the IP address of the VM you've just created (can find this either on the Azure portal or just using Powershell), it should display the computer name

Clean up

remove-azresourcegroup -name "todeleteRG" -force

Where to find the code
gist

Discussion (0)