How to Create an Azure VM with Power Shell (CLI)

Create a Virtual Machine in Azure:

az vm create \ 

--name MyWindowsVM \

--resource-group [Sandbox resource group name] \

--image Win2019Datacenter \

--admin-username azureuser

Run the following command to get IIS installed along with a default webpage:

az vm extension set \ 

--name CustomScriptExtension \

--version 1.9.4 \

--vm-name MyWindowsVM \

--resource-group Learn-e9a1-44fa-b74d-944d3acd382b \

--publisher Microsoft.Compute \

--settings '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'

Open port 80 on the web server to allow web traffic:

az vm open-port \

--name MyWindowsVM \

--resource-group Learn-7d66e471-e9a1-44fa-b74d-944d3acd382b \

--port 80

How to find the public IP address of the newly create virtual machine:

echo http://$(az vm list-ip-addresses \

--resource-group Learn-7d66e471-e9a1-44fa-b74d-944d3acd382b \

--name MyWindowsVM \

--query "[].virtualMachine.network.publicIpAddresses[*].ipAddress" \ --output tsv)

You can verify the web server is working by putting in the public IP address in a web browser All complete.

You have just created your first VM in Azure using PowerShell.

Posted in Azure, Microsoft and tagged , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *