Een VM maken in Azure met Powershell scheelt een hoop klikwerk en je kunt de code gebruiken om te automatiseren. Vandaar deze blogpost. Met dit script maak ik een VM aan met Windows 2012r2.
Je hebt wel Azure Powershell nodig. Deze kun je hier downloaden: http://aka.ms/webpi-azps
Log vervolgens eerst in op je azure subscription
Login-AzureRmAccount
$rgName = "Blonkit_Azure_VM02"
$location = "WestEurope"
$stName = "blonkitopslagaccount02"
$ipname = "AZUREVM02"
# create Resource Group
New-AzureRmResourceGroup -Name $rgName -Location $location
# new storage account
New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $stName -type Standard_LRS -Location $location
# new public ip
New-AzureRmPublicIpAddress -Name $ipname -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
# new network
$nicName = "AZUREVM02"
$VNET = Get-AzureRmVirtualNetwork -Name "S2S_Blonkit_001" -ResourceGroupName "Blonkit_Azure_VPN_Group001"
$SubnetID = (Get-AzureRmVirtualNetworkSubnetConfig -Name "SN_Blonkit_10.0.0.0" -VirtualNetwork $VNET).Id
New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $SubnetID -PublicIpAddressId "/subscriptions/cc6a3e15-8eb2-4a73-be3e-d9b319b5bf1d/resourceGroups/Blonkit_Azure_VM02/providers/Microsoft.Network/publicIPAddresses/AZUREVM02"
# create virtual machine
## PREP
### credentials
$cred = Get-Credential -Message "Type the name and password of the local administrator account."
### name vm
$vmName = "AZUREVM02"
### which VMsize?
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize "Standard_A1"
### computer name
$compName = "AZUREVM02"
### Operating system
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $compName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
### Operating system image
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"
### Add nic to vm
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id "/subscriptions/cc6a3e15-8eb2-4a73-be3e-d9b319b5bf1d/resourceGroups/Blonkit_Azure_VM02/providers/Microsoft.Network/networkInterfaces/AZUREVM02"
### where to store
$blobPath = "vhds/AZUREVM02.vhd"
$storageblob = Get-AzureRmStorageAccount -ResourceGroupName $rgName
$osDiskUri = $storageblob.PrimaryEndpoints.Blob.ToString() + $blobPath
### create the disk
$diskName = "AZUREVM02"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
## create the vm
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm
## start the new vm
Start-AzureRmVM -ResourceGroupName "Blonkit_Azure_VM02" -name "AzureVM02"
Mocht je nog vragen hebben, dan kun je reageren op dit bericht of mailen naar info@blonkit.nl