If you are looking to create Azure virtual machine using PowerShell, you have same two options as in classic Azure management portal. To start with, you need to first authenticate to Azure using PowerShell:
#Login into Microsoft Azure | |
add-azureaccount |
In the output, you can see current storage account associated with your subscription. If the CurrentStorageAccountName is empty, you’ll need to associate an storage account with your subscription. To do this, use Set-AzureSubscription cmdlet like this:
#Set default storage account for current azure subscription | |
Set-AzureSubscription -SubscriptionId <your subscription id> -CurrentStorageAccountName "qastoragenortheurope" |
If you want to create a new storage account you can use New-AzureStorageAccount cmdlet:
#Create a new storage account, if not already having one | |
$storageAccountName = "qastoragenortheurope" | |
$storageAccountLocation = "north europe" | |
$storageAccountType = "Standard_LRS" | |
New-AzureStorageAccount -StorageAccountName $storageAccountName -Location $storageAccountLocation -Type $storageAccountType |
After creating it, you can run Set-AzureSubscription cmdlet to associate it to your subscription.
Now you need to first get image name. For this, using this basic command:
$imageName = Get-AzureVMImage | | |
where { $_.ImageFamily -eq $imageFamily } | | |
sort PublishedDate -Descending | | |
select -ExpandProperty ImageName -First 1 |
Then you can use below script to create a quick Azure virtual machine for you:
#create quick azure vm | |
$adminUser = "itadmin" | |
$password = "itadmin@123" | |
$serviceName = "metavrsewebqa" | |
$location = "north europe" | |
$size = "Small" | |
$vmName = "metavrse-web-01" | |
$imageFamily = "Windows Server 2012 R2 Datacenter" | |
New-AzureQuickVM -Windows ` | |
-ServiceName $serviceName ` | |
-Name $vmName ` | |
-ImageName $imageName ` | |
-AdminUsername $adminUser ` | |
-Password $password ` | |
-InstanceSize $size |
Putting it all together:
#Login into Microsoft Azure | |
add-azureaccount | |
#Create a new storage account, if not already having one | |
$storageAccountName = "qastoragenortheurope" | |
$storageAccountLocation = "north europe" | |
$storageAccountType = "Standard_LRS" | |
New-AzureStorageAccount -StorageAccountName $storageAccountName ` | |
-Location $storageAccountLocation ` | |
-Type $storageAccountType | |
#Set default storage account for current azure subscription | |
Set-AzureSubscription -SubscriptionId <your subscription id> ` | |
-CurrentStorageAccountName "qastoragenortheurope" | |
#create quick azure vm | |
$adminUser = "itadmin" | |
$password = "itadmin@123" | |
$serviceName = "metavrsewebqa" | |
$location = "north europe" | |
$size = "Small" | |
$vmName = "metavrse-web-01" | |
$imageFamily = "Windows Server 2012 R2 Datacenter" | |
$imageName = Get-AzureVMImage | | |
where { $_.ImageFamily -eq $imageFamily } | | |
sort PublishedDate -Descending | | |
select -ExpandProperty ImageName -First 1 | |
New-AzureQuickVM -Windows ` | |
-ServiceName $serviceName ` | |
-Name $vmName ` | |
-ImageName $imageName ` | |
-AdminUsername $adminUser ` | |
-Password $password ` | |
-InstanceSize $size |
This will take a few minutes for virtual machine to get provisioned.
Let’s discuss on how to create a Azure virtual machine using advanced configuration. At this point, the basic steps first involves logging into Azure, setting storage subscription and fetching image details in a variable. Now for creating a virtual machine, use below commands:
$image = “someimage”
$vm1 = New-AzureVMConfig -Name “myvm” -InstanceSize “Small” -Image $image
At this point $vm1 is a persistent virtual machine configuration object that has the name, instance size and the image name set. Since my example is using an image and not booting from an existing disk I need to specify the provisioning configuration as well. For this, use Add-AzureProvisioningConfig cmdlet:
$adminUser = “itadmin”
$password = “itadmin@123”
$vm1 | Add-AzureProvisioningConfig -Windows -AdminUserName $adminUser
-Password $password
The above code passes the $vm1 configuration object to the Add-AzureProvisioningConfigcmdlet where it sets the provisioning configuration properties.
Common parameters for the -Windows and -WindowsDomain parameter sets.
- -AdminUserName – the administrative user account name for the virtual machine (cannot be administrator)
- -Password – the administrator password to set on boot.
- -DisableAutomaticUpdates – turns off Windows Update at provisioning time
- -DisableWinRMHttps – turns off remote PowerShell over https (cannot be turned back on from PowerShell so use with caution)
- -EnableWinRMHttp – turns on remote PowerShell over http (does not create a public endpoint – use for VM to VM PowerShell)
- -WinRMCertificate – an X509Certificate you may specify to use for remote PowerShell. If specified the cmdlets will automatically upload and deploy the certificate on your behalf. If not specified the service management API will create one for you.
- -X509Certificates – an array of X509Certificates. Like -WinRMCertificate the certificates specified here will be automatically uploaded and deployed to the VM for you. However, they will not be associated with WinRM/Remote PowerShell
- -NoExportPrivateKey – if specified with -X509Certificates the certificates will be deployed to the virtual machine without exporting the private key
- -TimeZone – boot the virtual machine up and automatically set its timezone.
- -ResetPasswordOnFirstLogon – The administrator account specified with -AdminUserName will require a password change on first login.
- -NoRDPEndpoint – an RDP endpoint will not be created
Now we’ll add a data disk to our virtual machine. For this, we’ll use below command:
$vm1 | Add-AzureDataDisk -CreateNew -DiskSizeInGB 500
-LUN 0 `
-DiskLabel “data”
Above command, add a data disk named “data” of size 500 GB to our virtual machine.
We can also add public endpoint to our virtual machine. Let’s say that we need to add a public endpoint for port 1433. For this, we can use below command:
$vm1 | Add-AzureEndpoint -Name “SQL” -Protocol tcp
-LocalPort 1433 `
-PublicPort 1433
In last, we’ll pass this configuration object to New-AzureVM cmdlet to create a virtual machine for us. The New-AzureVM cmdlet actually does the work of calling the Service Management API and creating the virtual machine.
$vm1 | New-AzureVM -ServiceName “my service” -Location “north europe”
A few notes about the New-AzureVM cmdlet:
- Passing -Location or -AffinityGroup tells the cmdlet you want it to create the cloud service
- Not passing -Location or -AffinityGroup tells the cmdlet that you want it to create the virtual machine in an existing cloud service
We’ll skip specifying the network configuration for now.
Putting this all together in a nice script:
#Login into Microsoft Azure | |
add-azureaccount | |
#Create a new storage account, if not already having one | |
$storageAccountName = "qastoragenortheurope" | |
$storageAccountLocation = "north europe" | |
$storageAccountType = "Standard_LRS" | |
New-AzureStorageAccount -StorageAccountName $storageAccountName ` | |
-Location $storageAccountLocation ` | |
-Type $storageAccountType | |
#Set default storage account for current azure subscription | |
Set-AzureSubscription -SubscriptionId <your subscription id> ` | |
-CurrentStorageAccountName "qastoragenortheurope" | |
#create azure vm | |
$adminUser = "itadmin" | |
$password = "itadmin@123" | |
$serviceName = "metavrseweb" | |
$location = "north europe" | |
$Size = (Get-AzureRoleSize | Where { $_.InstanceSize –eq "Standard_D1" }).InstanceSize | |
$vmName = "metavrse-web-02" | |
$imageFamily = "Windows Server 2012 R2 Datacenter" | |
$imageName = Get-AzureVMImage | | |
where { $_.ImageFamily -eq $imageFamily } | | |
sort PublishedDate -Descending | | |
select -ExpandProperty ImageName -First 1 | |
New-AzureVMConfig -Name $vmName ` | |
-InstanceSize $size ` | |
-ImageName $imageName | | |
Add-AzureProvisioningConfig -Windows ` | |
-AdminUsername $adminUser ` | |
-Password $password | | |
Add-AzureDataDisk -CreateNew ` | |
-DiskSizeInGB 500 ` | |
-LUN 0 ` | |
-DiskLabel "data" | | |
Add-AzureEndpoint -Name "SQL" ` | |
-Protocol tcp ` | |
-LocalPort 1433 ` | |
-PublicPort 1433 | | |
New-AzureVM -ServiceName $serviceName ` | |
-Location $location |
[…] Now, we only need to add only one line to script used in our earlier post: […]
LikeLike
[…] thing first, we need to create a Azure virtual machine with Windows Server 2016 as Operating system installed on it. Also it is to be noted that the Azure […]
LikeLike