Azure – Create a Virtual Machine with Azure Power Shell
Hello Friends,
Hope you all are doing good !!!
Let’s continuing with our posts on Azure Exam : Developing Microsoft Azure Solutions.
In our last blog, we have discussed about Microsoft service Command Line Interface.In that blog we have discussed how to manage and execute powershell command by browser without configuring the local environment.Today we will discuss, how to configure local environment to support Azure PowerShell script and will go through a example, where we will create a new Azure Virtual Machine using Azure Power Shell from local environment.
If we have not configured the Azure PowerShell yet, Let’s go through the following steps an configure it before proceeding with our example.
- To proceed with the Azure PowerShell configuration, let’s have a little information from the this link.From this link we found, that we can download a MSI file and click on it to install the Azure PowerShell. So please download the Azure-Cmdlets-6.11.0.23555-x64.msi and click on it to continue with the installation, as shown in the following figure.
Figure 1: Azure Certification 70-532 – Download MSI File For Azure PoserShell - Allow to proceed with the installation, as in following figure.
Figure 2: Azure Certification 70-532 – Allow to proced with Installation - Once the Azure PowerShell installation done, we will lunch Window Powershell and try to connect to our azure account by selecting Always run ,if there is no security issue, as in following figure.
Figure 3: Azure Certification 70-532 – Connect ot our Azure Accoun - Once every thing went well, it will ask us to provide our azure subscription credentials to connect to our Azure account and once, we are authenticated, we are all set to go with our example, as in following figure.
Note :
After November 2018, there will be no new features or cmdlets added to the AzureRM module. AzureRM will continue to be supported and receive bug fixes. New features will be provided in the Az module, which will reach 1.0 at the same time. Az has a backwards compatibility mode with AzureRM, and is designed to be easy to switch to. To learn more about this new module and how to upgrade, see:
- Introducing the Azure PowerShell Az module
- Install Azure PowerShell Az module
- Migrate to the new Azure PowerShell Az module
If you have deployments that use the classic deployment model that cannot be converted, you can install the Service Management version of Azure PowerShell. For more information, see Install the Azure PowerShell Service Management module.
Now We are ready with our local configuration to execute any powershell script against our Azure Account.as we discussed earlier, today we will move and try to create a new Virtual Machine server using a powershell script.We know that a virtual machine can not be created alone.It has many dependencies like , Resource Group, Stoage Account, Network configuration,IP configuration etc.So before creating the VM , we need to make sure that, all the prerequisites must be in place.
I have downloaded a powershell script created by Mike Pfeiffer and did a little modification.In this powershell script Mike has taken care of all the required prerequisites.
So lets start with the example and execute the following powershell script by providing required parameters.
[CmdletBinding()] Param ( [Parameter(Mandatory=$true)] $VMName, [Parameter(Mandatory=$true)] $ResourceGroupName, [Parameter(Mandatory=$false)] $Location = 'West India', [Parameter(Mandatory=$false)] $StorageAccountName = (Get-Random -Minimum 11111 -Maximum 99999999), [Parameter(Mandatory=$false)] $NetworkAddressPrefix = '10.0.0.0/16', [Parameter(Mandatory=$false)] $SubnetAddressPrefix = '10.0.1.0/24', [System.Management.Automation.PSCredential] [Parameter(Mandatory=$true)] $Credential ) Write-Verbose "Creating Resource Group if not exist" New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location $storageParams = @{ Name = $StorageAccountName ResourceGroupName = $ResourceGroupName Type = 'Standard_LRS' Location = $Location } Write-Verbose "Creating Storage Account" $storageAccount = New-AzureRmStorageAccount @storageParams $subnetParams = @{ Name = "$ResourceGroupName" + "-Subnet" AddressPrefix = $SubnetAddressPrefix } Write-Verbose "Creating new network subnet config" $subnet = New-AzureRmVirtualNetworkSubnetConfig @subnetParams $vnetParams = @{ Name = "$ResourceGroupName" + "-vNET" ResourceGroupName = $ResourceGroupName Location = $Location AddressPrefix = $NetworkAddressPrefix Subnet = $subnet } Write-Verbose "Creating VNET" $vnet = New-AzureRmVirtualNetwork @vnetParams $nicName = "$VMName-NIC1" $publicIpParams = @{ Name = $nicName ResourceGroupName = $ResourceGroupName Location = $Location AllocationMethod = 'Dynamic' } Write-Verbose "Creating public ip config" $publicIP = New-AzureRmPublicIpAddress @publicIpParams $nicParams = @{ Name = $nicName ResourceGroupName = $ResourceGroupName Location = $Location SubnetId = $vnet.Subnets[0].Id PublicIpAddressId = $publicIP.Id } Write-Verbose "Creating network interface config" $nic = New-AzureRmNetworkInterface @nicParams Write-Verbose 'Creating vm config' $vm = New-AzureRmVMConfig -VMName $VMName -VMSize 'Basic_A1' $osParams = @{ VM = $vm Windows = $true ComputerName = $VMName Credential = $Credential ProvisionVMAgent = $true EnableAutoUpdate = $true } Write-Verbose 'Setting OS image config' $vm = Set-AzureRmVMOperatingSystem @osParams $sourceImageParams = @{ VM = $vm PublisherName = 'MicrosoftWindowsServer' Offer = 'WindowsServer' Skus = '2012-R2-Datacenter' Version = 'latest' } $vm = Set-AzureRmVMSourceImage @sourceImageParams Write-Verbose 'Adding network interface to VM' $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id $osDiskName = "$VMName-OS-DISK" $osDiskParams = @{ Name = $osDiskName VM = $vm VhdUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/$osDiskName.vhd" CreateOption = 'FromImage' } $vm = Set-AzureRmVMOSDisk @osDiskParams Write-Verbose 'Creating VM Server' New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $vm
As Shown in the following figure, We need to provide the required parameters and it will craete all required resources in before hand and then it will proceed to create a new ManasTestVM, as shown in the following figure.
Figure 5: Azure Certification 70-532 – Executing Powershell Script to create new VM
When executing the script file, you might face the following script not digitally signed issue, as shown in the following figure.In that case, we can set “Set-ExecutionPolicy” to unrestricted if it is safe, as shown in the following figure.
Figure 6: Azure Certification 70-532 – Set Execution policy to Unrestricted
Once we have done with the above configuration, we can re execute execute the script and it will ask you the User Name and Password for the new VM and we need to provide the new username and password, as shown in the following figure.
Figure 7: Azure Certification 70-532 – Asking VM creadentials
After Some time, we can see our new VM is ready with all configurations, as shown in the following figure.
Figure 8: Azure Certification 70-532 -VM Creatation task is done
Figure 9: Azure Certification 70-532 -VM Creatation successfully done
Thanks for reading 🙂
Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.
Stay tuned on Knowledge-Junction, will come up with more such articles.
More Posts On Exam : Developing Microsoft Azure Solutions :
Azure – Create New Azure Virtual Machine
Azure – Managing Azure Virtual Machine Disks
Azure – Create An Azure Storage Account.
Azure – Configuring Shared Storage Using Azure File Service
Azure – Working with Azure CLI
Azure – Deploying a Web App to Azure Virtual Machine with Web Deploy method
Azure – Remote debugging on an Azure VM
Azure – Azure Automation State Configuration
Azure – Configure Azure PowerShell Desired State Configuration (DSC) Through Azure Portal
6 Responses
[…] Azure – Certification 70-532-Part 6 – Create a Virtual Machine with Azure Power Shell […]
[…] Azure – Certification 70-532-Part 6 – Create a Virtual Machine with Azure Power Shell […]
[…] Azure – Certification 70-532-Part 6 – Create a Virtual Machine with Azure Power Shell […]
[…] Azure – Certification 70-532-Part 6 – Create a Virtual Machine with Azure Power Shell […]
[…] Azure – Certification 70-532-Part 6 – Create a Virtual Machine with Azure Power Shell […]
[…] Azure – Certification 70-532-Part 6 – Create a Virtual Machine with Azure Power Shell […]