Last active
April 5, 2017 04:46
-
-
Save mjohnsonengr/c4ade2242e5720a718e7 to your computer and use it in GitHub Desktop.
Creating Azure Snapshots -- from http://www.westerndevs.com/azure-snapshots/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set variable values | |
$resourceGroupName = "SnapshotDemo" | |
$location = "West US" | |
$vmName = "SnapshotDemo" | |
$vmSize = "Standard_A1" | |
$vnetName = "SnapshotDemo" | |
$nicName = "snapshotdemo98" | |
$dnsName = "snapshotdemo" | |
$diskName = "SnapshotDemo" | |
$storageAccount = "snapshotdemo5062" | |
$storageAccountKey = "<Insert Storage Account Key Here>" | |
$subscriptionName = "Visual Studio Enterprise with MSDN" | |
$publicIpName = "SnapshotDemo" | |
$diskBlob = "$diskName.vhd" | |
$backupDiskBlob = "$diskName-backup.vhd" | |
$vhdUri = "https://$storageAccount.blob.core.windows.net/vhds/$diskBlob" | |
$subnetIndex = 0 | |
# login to Azure | |
Add-AzureRmAccount | |
Set-AzureRMContext -SubscriptionName $subscriptionName | |
# create backup disk if it doesn't exist | |
Stop-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName -Force -Verbose | |
$ctx = New-AzureStorageContext -StorageAccountName $storageAccount -StorageAccountKey $storageAccountKey | |
$blobCount = Get-AzureStorageBlob -Container vhds -Context $ctx | where { $_.Name -eq $backupDiskBlob } | Measure | % { $_.Count } | |
if ($blobCount -eq 0) | |
{ | |
$copy = Start-AzureStorageBlobCopy -SrcBlob $diskBlob -SrcContainer "vhds" -DestBlob $backupDiskBlob -DestContainer "vhds" -Context $ctx -Verbose | |
$status = $copy | Get-AzureStorageBlobCopyState | |
$status | |
While($status.Status -eq "Pending"){ | |
$status = $copy | Get-AzureStorageBlobCopyState | |
Start-Sleep 10 | |
$status | |
} | |
} | |
# delete VM | |
Remove-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName -Force -Verbose | |
Remove-AzureStorageBlob -Blob $diskBlob -Container "vhds" -Context $ctx -Verbose | |
Remove-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroupName -Force -Verbose | |
Remove-AzureRmPublicIpAddress -Name $publicIpName -ResourceGroupName $resourceGroupName -Force -Verbose | |
# copy backup disk | |
$copy = Start-AzureStorageBlobCopy -SrcBlob $backupDiskBlob -SrcContainer "vhds" -DestBlob $diskBlob -DestContainer "vhds" -Context $ctx -Verbose | |
$status = $copy | Get-AzureStorageBlobCopyState | |
$status | |
While($status.Status -eq "Pending"){ | |
$status = $copy | Get-AzureStorageBlobCopyState | |
Start-Sleep 10 | |
$status | |
} | |
# recreate VM | |
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName | |
$pip = New-AzureRmPublicIpAddress -Name $publicIpName -ResourceGroupName $resourceGroupName -DomainNameLabel $dnsName -Location $location -AllocationMethod Dynamic -Verbose | |
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroupName -Location $location -SubnetId $vnet.Subnets[$subnetIndex].Id -PublicIpAddressId $pip.Id -Verbose | |
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize | |
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id | |
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $vhdUri -CreateOption attach -Windows | |
New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm -Verbose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
amazing script ... thanks for sharing....
i have one question. what is the easiest way to get the values that we need to incorporate in variables ?