96
CHAPTER 3 | Azure Virtual
Machines
# !!!! This example assumes the new VM is in a different resource group
and storage account from
the captured VM. !!!!
$srcKey = Get-AzureRmStorageAccountKey -StorageAccountName $capturedImageStorageAccount -
ResourceGroupName $catpuredImageStorageAccountResourceGroup
$srcContext = New-AzureStorageContext -StorageAccountName $capturedImageStorageAccount -
StorageAccountKey $srcKey.Key1
# **** Create the Network Resources ****
$publicIp = New-AzureRmPublicIpAddress -Name "MyPublicIp01" `
-ResourceGroupName $resourceGroupName `
-Location $location -AllocationMethod Dynamic
$subnetConfiguration = New-AzureRmVirtualNetworkSubnetConfig -Name "MySubnet" `
-AddressPrefix "10.0.0.0/24"
$virtualNetworkConfiguration = New-AzureRmVirtualNetwork -Name "MyVNET" `
-ResourceGroupName $resourceGroupName `
-Location $location `
-AddressPrefix "10.0.0.0/16" `
-Subnet $subnetConfiguration
$nic = New-AzureRmNetworkInterface -Name "MyServerNIC01" `
-ResourceGroupName $resourceGroupName `
-Location $location `
-SubnetId $virtualNetworkConfiguration.Subnets[0].Id `
-PublicIpAddressId $publicIp.Id
# **** Create the new Azure VM ****
# Get the admin credentials for the new VM
$adminCredential = Get-Credential
# Get the storage account for
the captured VM image
$storageAccount = New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name
"ezazurevm2016" -Location $location -Type Standard_LRS
# Copy the captured image from the source storage account to the destination storage account
$destImageName = $capturedImageUri.Substring($capturedImageUri.LastIndexOf('/') + 1)
New-AzureStorageContainer -Name "images" -Context $storageAccount.Context
Start-AzureStorageBlobCopy -AbsoluteUri $capturedImageUri -DestContainer "images" -DestBlob
$destImageName -DestContext $storageAccount.Context -Context $srcContext -Verbose -Debug
Get-AzureStorageBlobCopyState -Context $storageAccount.Context -Container "images" -Blob
$destImageName -WaitForComplete
# Build the URI for the image in the new storage account
$imageUri = '{0}images/{1}' -f $storageAccount.PrimaryEndpoints.Blob.ToString(), $destImageName
# Set the VM configuration details
$vmConfig = New-AzureRmVMConfig -VMName "ezazurevm10" -VMSize "Standard_D1"
# Set the operating system details
$vm = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmConfig.Name -
Credential $adminCredential -TimeZone "Eastern Standard Time" -ProvisionVMAgent -EnableAutoUpdate
97
CHAPTER 3 | Azure Virtual Machines
# Set the NIC
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
#
Create the OS disk URI
$osDiskUri = '{0}vhds/{1}_{2}.vhd' -f $storageAccount.PrimaryEndpoints.Blob.ToString(),
$vm.Name.ToLower(), ($vm.Name + "_OSDisk")
# Configure the OS disk to use the previously saved image
$vm = Set-AzureRmVMOSDisk -vm $vm -Name $vm.Name -VhdUri $osDiskUri -CreateOption FromImage -
SourceImageUri $imageUri -Windows
# Create the VM
New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm
For Linux VMs, the capture process is similar. Although you can use PowerShell to capture the VM, a
common approach is to use the Azure CLI. You would use three basic Azure CLI commands:
azure
vm stop -g -n
azure vm generalize -g -n
azure vm capture -t
Do'stlaringiz bilan baham: