Last active
August 25, 2016 10:48
-
-
Save darrenjrobinson/5b8bc3e41d2b2068250fe5d5ada82836 to your computer and use it in GitHub Desktop.
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
# Resource Group of the busted VM | |
$ResourceGroup = 'mydevresourcegroup' | |
# The busted VM Name | |
$brokenVM ='my-failed-azurerm-virtualmachine-name' | |
# Subnet the busted VM is located in | |
$brokenVMSubnet = 'subnet-my-failed-azurermvm-is-located-on' | |
# Import AzureRM Module | |
Import-Module AzureRM | |
# see if we already have a session. If we don't authN | |
if (!$AzureRMAccount.Context.Tenant) { | |
$AzureRMAccount = Add-AzureRmAccount | |
} | |
# Get Sub Info | |
$SubscriptionName = Get-AzureRmSubscription | sort SubscriptionName | Select SubscriptionName | |
$TenantId = $AzureRMAccount.Context.Tenant.TenantId | |
Select-AzureRmSubscription -TenantId $TenantId | |
# Get details about the broken VM | |
$bustedVM = get-azurermvm -ResourceGroupName $ResourceGroup -Name $brokenVM | |
# OS Disk that we want to attach to the newVM as the OSDisk | |
$OSDisk = $bustedVM.StorageProfile.OsDisk.Vhd | |
$OSDiskName = $bustedVM.StorageProfile.OsDisk.Name | |
# Storage Account Stuff | |
# Get Storage URI from Broken VM | |
[uri]$storageUri = $bustedVM.DiagnosticsProfile.BootDiagnostics.StorageUri | |
# Get the StorageAccountName from the Storage URI | |
$storageAccountName = $storageUri.Host.Split('.').Item(0) | |
# Get the StorageAccount from the Storagename and Resource Group | |
$storageAccount = Get-AzureRMStorageAccount -ResourceGroupName $ResourceGroup -Name $storageAccountName | |
$storageContext = (Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroup -Name $storageAccountName).Context | |
# Copy the failed VM OS Disk | |
# Get the Blob Info for the VHD attached the failed VM | |
[uri]$vhdContainer = $OSDisk.uri | |
[string]$vhdContainer = $vhdContainer.Segments[1] | |
$vhdContainer = $vhdContainer.Substring(0, $vhdContainer.Length - 1) | |
[string]$vhdBlob = Split-Path -Leaf $OSDisk.uri | |
[string]$vhdDestBlob = $brokenVM+'-'+(Get-Date -format "yyyyMMdd")+'.vhd' | |
# Copy the VHD to a new name as it's going to the same blog location | |
$BlobCopyResult = Start-AzureStorageBlobCopy -SrcBlob $vhdBlob -SrcContainer $vhdContainer -DestContainer $vhdContainer -DestBlob $vhdDestBlob -Context $storageContext -DestContext $storageContext | |
# Wait for it to copy (if it is really big) | |
$status = $BlobCopyResult | Get-AzureStorageBlobCopyState | |
$status | |
# Check progress of the copy and wait until complete (if it is big) | |
While ($status.Status -eq "Pending") { | |
$status = $BlobCopyResult | Get-AzureStorageBlobCopyState | |
$status | |
Start-Sleep 10 | |
} | |
# URI for copied VHD which we'll attached as the OSDrive for the newVM | |
$NewVMDiskURI = $storageUri.AbsoluteUri + $vhdContainer +'/' + $vhdDestBlob | |
# Get all the other inputs for the new VM to clone the busted one | |
# VM Size (keep the same as the busted one) | |
$VMSize = $bustedvm.HardwareProfile.VmSize | |
# Name with 2 on the end | |
$VMName = $bustedvm.Name+'2' | |
# Location we'll keepp the same | |
$location = $bustedVM.Location | |
# Start the settings for the New VM | |
$NewVM = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize | |
$NewVM | Set-AzureRmVMOSDisk -VhdUri $NewVMDiskURI -Name $OSDiskName -CreateOption attach -Windows -Caching ReadWrite | |
# VirtNet | |
$VirtNet = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup | |
# Subnet | |
$SubnetID = (Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $VirtNet -Name $brokenVMSubnet).Id | |
# Create new Publich IP and create a new NIC | |
$pip = New-AzureRmPublicIpAddress -Name "$VMName-IP2" -ResourceGroupName $ResourceGroup -Location $location -AllocationMethod Dynamic | |
$nic = New-AzureRmNetworkInterface -Name "$VMName-NIC2" -ResourceGroupName $ResourceGroup -Location $location -PublicIpAddressId $pip.Id -SubnetId $SubnetID | |
# Add the new NIC to the new VM | |
$NewVM = Add-AzureRmVMNetworkInterface -VM $NewVM -Id $nic.Id | |
# Tell Azure to make it happen | |
New-AzureRmVM -ResourceGroupName $ResourceGroup -Location $location -vm $NewVM -Verbose | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment