Thursday, 13 October 2016

Create Azure Windows VM using Powershell.

 Create a plenty new VM in Azure ARM portal, just you need to do is copy blow code and paste it into you powershell ISE .. make sure Powershell is opened as Administrator mode.
And in Global variables you just need to change the values according to your needs.
# ------------------------------------------Create New VM -----------------------------------------------

# Sign-in with Azure account credentials

Login-AzureRmAccount

# Select your Azure Subscription.

$subscriptionId =
    (Get-AzureRmSubscription |
     Out-GridView `
        -Title "Select an Azure Subscription ..." `
        -PassThru).SubscriptionId

Select-AzureRmSubscription `
    -SubscriptionId $subscriptionId

## Global Variables.

$ResourceGroupName = "srcresourcegrp01"
$location = "Southeastasia"

## Storage
$storageName = "srcstorage01"
$storageType = "Standard_A1"

## Network
$nicname = "srcnic"
$subnetName = "srcsubnet"
$vnetName = "srcvnet"
$vnetAddressPrefix = "10.0.0.0/16"
$vnetSubnetAddressPrefix = "10.0.0.0/24"
$publicIPName = "srcpublicip"

## Computer
$vmName = "srcvm"
$computerName = "srccomp"
$vmSize = "Standard_A1"
$osDiskName = $vmName + "osDisk"
$blobPath = "vhds/srcosDisk.vhd"

###############################################################################################################

# Create New Resource Group
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $location

#Create New Storage account
Get-AzureRmStorageAccountNameAvailability $storageName
$storageAcc = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $storageName -SkuName "Standard_LRS" -Kind "Storage" -Location $location

#Create a virtual network
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $vnetSubnetAddressPrefix
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $ResourceGroupName -Location $location -AddressPrefix $vnetAddressPrefix -Subnet $singleSubnet

#Create a public IP address and network interface
$pip = New-AzureRmPublicIpAddress -Name $publicIPName -ResourceGroupName $ResourceGroupName -Location $location -AllocationMethod Dynamic
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroupName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id

#Create a virtual machine

#The password must be at 12-123 characters long and have at least one lower case character, one upper case character, one number, and one special character.
$cred = Get-Credential -Message "Type the User name and password of the local administrator account."


$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize

$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

# Specify Blob Path

$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + $blobPath
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption fromImage

#Finally, create the virtual machine.

New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $location -VM $vm

# The End.


Once you get success output, means your VM has been created and ready to use it.




No comments:

Post a Comment

Get SSL Certificate Expiry Notification on Mail.

 There are multiple ways to get SSL Certification expiry details/alert. We all know if our SSL certificate get expire, how critical situatio...