Mastering Windows Server 2016 Hyper-V

(Romina) #1

to the variable $AzureImage. The VM creates a new public IP address and assigns it to
the VM.


$rgname = 'RG-SCUSA'
$vmsize = 'Standard_A2'
$vmname = 'VM-TestVM'


Setup Storage


$stoname = 'sa-lrsscussav'
$stotype = 'Standard_LRS'


Virtual Network


$virtname = 'VN-Net1'
$stoaccount = Get-AzureRmStorageAccount -ResourceGroupName $rgname `
-Name $stoname


Create VM Object


$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize


#Get the network object
$vnet = Get-AzureRmVirtualNetwork -Name ($virtname) `
-ResourceGroupName $rgname
$subnetId = $vnet.Subnets[0].Id


#Create a Public IP address
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $rgname -Name "vip1" -Location $loc -AllocationMethod Dynamic
-DomainNameLabel $vmname.ToLower()
#Create a new NIC using the virtual network and the Public IP
$nic = New-AzureRmNetworkInterface -Force -Name ('nic' + $vmname) -ResourceGroupName $rgname -Location $loc
-SubnetId $subnetId -PublicIpAddressId $pip.Id


Add NIC to VM configuration


$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
#Setup the OS disk
$osDiskName = $vmname+'_osDisk'
$osDiskCaching = 'ReadWrite'
$osDiskVhdUri =
"https://$stoname.blob.core.windows.net/vhds/"+$vmname+"_os.vhd"


Setup OS & Image


$user = "localadmin"
$password = 'Pa55word5'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user,
$securePassword)
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmname -Credential $cred $vm = Set-AzureRmVMSourceImage -VM $vm
-PublisherName $AzureImage.PublisherName -Offer $AzureImage.Offer -Skus $AzureImage.Skus
-Version $AzureImage.Version
$vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri `
-name $osDiskName -CreateOption fromImage -Caching $osDiskCaching


Create Virtual Machine


New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm


Once all of the PowerShell runs, you will have a new VM. To view all VHDs in a
storage account, use the following code:

Free download pdf