Skip to content

Instantly share code, notes, and snippets.

@vijayjt
Last active April 10, 2017 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijayjt/1b716df50051194960b8a7febb32cd3e to your computer and use it in GitHub Desktop.
Save vijayjt/1b716df50051194960b8a7febb32cd3e to your computer and use it in GitHub Desktop.
PowerShell code to create a Windows Server 2008 R2 VM in Azure (for ASE and AD blog post)
# Select a subscritpion
Set-AzureRmContext -SubscriptionName (Get-AzureRmSubscription | Out-GridView -PassThru).SubscriptionName
$Location = 'North Europe'
# Create a resource group for the VM and storage account
$CoreInfraResourceGroupName = 'VM-RG-0001'
New-AzureRmResourceGroup -Name $CoreInfraResourceGroupName -Location $Location
$CoreInfraVMStorageAccount = '0048vhdstorage'
New-AzureRmStorageAccount -ResourceGroupName $CoreInfraResourceGroupName -Name $CoreInfraVMStorageAccount -Type Standard_LRS -Location $Location
# Use existing VNET
$VNETResourceGroupName = 'VNET-RG-001'
$VNETName = 'VNET-001'
$AccountName = 'my-azure-admin'
$vmpwd = 'ReplaceWithAStrongPassword'
$VMSize = 'Standard_A2'
# Get Windows Server Image Details
$offer = (Get-AzureRmVMImageOffer -Location $Location -PublisherName $publisher | Where-Object Offer -eq 'WindowsServer' ).Offer
$sku = (Get-AzureRmVMImageSku -Location $Location -Offer $offer -PublisherName $publisher | Where-Object Skus -eq '2008-R2-SP1').Skus
$imageid = (Get-AzureRmVMImage -Location $Location -Offer $offer -PublisherName $publisher -Skus $sku | Sort-Object Version -Descending)[0].Id
$version = (Get-AzureRmVMImage -Location $Location -Offer $offer -PublisherName $publisher -Skus $sku | Sort-Object Version -Descending)[0].Version
$VMName = 'AZ-DC-001'
$ComputerName = $VMName
$OSDiskName = $VMName + 'osDisk'
$subnetname = 'SN-MGT-01'
$InterfaceName = 'nic-' + $VMName;
# Get the storage account
$StorageAccount = Get-AzureRMStorageAccount -Name $CoreInfraVMStorageAccount -ResourceGroupName $CoreInfraResourceGroupName
# Configure vnet, subnet and nic
$vnet = Get-AzureRmVirtualNetwork -Name $VNETName -ResourceGroupName $VNETResourceGroupName
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetname -VirtualNetwork $vnet
$Interface = New-AzureRmNetworkInterface -Name $InterfaceName -ResourceGroupName $CoreInfraResourceGroupName -Location $Location -SubnetId $subnet.Id
# Create a credential object
$SecurePassword = ConvertTo-SecureString $vmpwd -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($AccountName, $SecurePassword);
# Set VM configuration
$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $ComputerName -Credential $Credential `
-ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName $publisher -Offer $offer -Skus $sku -Version 'latest'
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $Interface.Id
$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + 'vhds/' + $OSDiskName + '.vhd'
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption FromImage
## Create the VM
New-AzureRmVM -ResourceGroupName $CoreInfraResourceGroupName -Location $Location -VM $VirtualMachine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment