Deploy Azure VM to Powershell

In this post we will teach you how to create a VM in Azure using the Powershell script. I created this a few months ago as a team with Thiago Alencar to meet a need in the company I worked on to deploy many similar virtual machines on azure, which come from a predefined list of VMs in a csv file.

Read more:

Manage VMs in your Hyper-V 3.0 environment
Configuring Azure AD Connect step by step
What are the 4 types of Microsoft Active Directory
Cloud storage and its benefits
Learn how to create VMs on azure from the command line

Let's start. You should start to open the Windows PowerShell ISE tool

Once opened, we should start to code, and your first step should connect to your signature

Connect-AzAccount

PS - for the above command, a window will appear asking for your azure credentials

If you have more than one subscription, keep these commands in mind and select the correct one you want, for example, I will choose my Visual Studio subscription.

Get-AzSubscription -SubscriptionName "Visual Studio Enterprise with MSDN" | Select-AzSubscription

As we well know, all the resources in blue must be in a resource group (TCRG in my case) and must belong to a certain location, to avoid retyping repeatedly, I will put them in variables and reuse them if necessary .

$ResourceGroupName = "TCRG" $Location = "northeurope" New-AzResourceGroup -Name $ResourceGroupName -Location $Location

If we go to the Azure portal, we will look at a RG (resource group) created there

From here, I'm going to create network stuff like vnet, subnet, NSG and subnet input and output rules.

$SubnetName = $ResourceGroupName + "subnet" $VnetName = $ResourceGroupName + "vnet" $SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix 192.168.1.0NAME-1Name_TR2 -Subnet $SubnetConfig $NsgRuleInbound = New-AzNetworkSecurityRuleConfig -Name "NsgRuleInbound" -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefule * EntranceResponseRange -Direction Outbound -Priority 101 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange * -Access Allow $NsgName = $ResourceGroupName + "nsg" $Nsg = New-AzNetworkSecurityNameTPTPTRResourceLoup 1 n -Name $NsgName -SecurityRules $NsgRuleInbound, $NsgRuleOutbound

PS - Please pay attention that I allowed any connection (input and output), DO NOT DO THIS IN PROD.

#Profissa: Take a professional course!
Ensure a certified course in IT and Software through the online study platform Udemy.
click here and check out all the latest technology courses!

So far, our deployment looks like:

PS - note that I haven't created the IP yet, it will be done within a Foreach later.

Once the network setup is complete, I will get all the VM properties from my VMList.csv file that are being kept in the path c: \ temp \. Let's take a look inside this file

PowerShell will load all properties from this file into an object. Within the for each statement, I will create the IP, interface, SQL server offer, SQL edition, etc.

$GetVmList = Import-Csv "C: \ temp \ VMList.csv" foreach ($vml in $GetVmList) {$SecurePassword = ConvertTo-SecureString $vml. , $securePassword) $PipName $ResourceGroupName + = $ (Get-Random) = $Pip New AzPublicIpAddress -ResourceGroupName $ResourceGroupName -location $Location -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name = $PipName $Interface New AzNetworkInterface -Name $vml.Interface -ResourceGroupName $ResourceGroupName -location $Location - SubnetId $VNet.Subnets [0] .Id -PublicIpAddressId $Pip.Id -NetworkSecurityGroupId $Nsg.Id $VMConfig = New-AzVMConfig -VMName $vml.VMName -VMSize $vize.TPMT Set-AzVMOperatingSystem -Windows -ComputerName $vml.VMName -Credential $Cred -ProvisionVMAgent -EnableAutoUpdate | Set-AzVMSourceImage -PublisherName "MicrosoftSQLServer" -Offer "SQL2016SP2-WS2012R2" -Skus "SQLDEV" -Version "latest" | Add-AzVMNetworkInterface -Id $Interface.Id New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VMConfig}

The above command will take a few minutes to run, when you're done, you'll see a message like this:

Remember that I went through the Azure Powershell interface to remove these values to add them to my solution, if you need anything other than that, consult Microsoft documentation to find out what you will need to implement.

#Profissa: Take a professional course!
Ensure a certified course in IT and Software through the online study platform Udemy.
click here and check out all the latest technology courses!

This is how our solution should be at the end of the deployment

Click on the SQLVM01 VM> connect and download the RDP to your machine and register with the credentials previously created in the .csv file

Your machine will be ready to use! I hope you enjoyed it and even more!

Was there another way to implement and improve this solution? Of course, I could create many "IFs" to validate whether something exists or not and so on, but it was not necessary in my scenario, feel free to use this solution and improve it.

Was this article helpful?

To maintain a quality standard for you, we have invested in a great hosting plan, Paid CDN, Website Optimization Plugins, etc ...

Help us to keep the project active! 

Follow the news in real time. Follow our Instagram profile..

Felipe Santos
Felipe Santos is a Cloud and Security Architect, with experience in Windows Server, Cluster, Storages, Backups Veeam and Office 365 environments.
en_USEnglish

UP TO 90% DISCOUNT

TAKE OFF YOUR CAREER !!

Do you want to upgrade your career? 

Invest in yourself and get ahead! Get that dream job in 2022!