Created
April 14, 2017 12:50
-
-
Save KacperMucha/ac55521d994d34ffc22361c464bc3199 to your computer and use it in GitHub Desktop.
Recreate VM in availability set
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
# Sign-in to Azure via Azure Resource Manager | |
#Login-AzureRmAccount | |
# Select Azure Subscription | |
$subscriptionId = | |
( Get-AzureRmSubscription | | |
Out-GridView ` | |
-Title "Select an Azure Subscription ..." ` | |
-PassThru | |
).SubscriptionId | |
Select-AzureRmSubscription ` | |
-SubscriptionId $subscriptionId | |
# If needed, register ARM core resource providers | |
Register-AzureRmResourceProvider ` | |
-ProviderNamespace Microsoft.Compute | |
Register-AzureRmResourceProvider ` | |
-ProviderNamespace Microsoft.Storage | |
Register-AzureRmResourceProvider ` | |
-ProviderNamespace Microsoft.Network | |
Get-AzureRmResourceProvider | | |
Select-Object ` | |
-Property ProviderNamespace ` | |
-ExpandProperty ResourceTypes | |
# Select Azure Resource Group in which existing VNET is provisioned | |
$rgName = | |
( Get-AzureRmResourceGroup | | |
Out-GridView ` | |
-Title "Select an Azure Resource Group ..." ` | |
-PassThru | |
).ResourceGroupName | |
# Select VM to re-provision | |
$vmName = | |
( Get-AzureRmVm ` | |
-ResourceGroupName $rgName | |
).Name | | |
Out-GridView ` | |
-Title "Select a VM ..." ` | |
-PassThru | |
$vm = | |
Get-AzureRmVm ` | |
-ResourceGroupName $rgName ` | |
-Name $vmName | |
$location = | |
$vm.Location | |
# Create a new Azure availability set | |
$asName = | |
Read-Host ` | |
-Prompt "Enter a new Availability Set name" | |
$as = | |
New-AzureRmAvailabilitySet ` | |
-Name $asName ` | |
-ResourceGroupName $rgName ` | |
-Location $location | |
# Stop and Deprovision existing Azure VM, retaining Disks | |
$vm | Stop-AzureRmVm -Force | |
$vm | Remove-AzureRmVm -Force | |
# Set VM config to include new Availability Set | |
#$asRef = New-Object Microsoft.Azure.Management.Compute.Models.AvailabilitySetReference | |
#$asRef.ResourceUri = $as.Id | |
#$vm.AvailabilitySetReference = $asRef # To remove VM from Availability Set, set to $null | |
#$asId = (Get-AzureRmAvailabilitySet -ResourceGroupName $rgName -Name $asName).Id | |
$vm.AvailabilitySetReference = $as.Id | |
# Clean-up VM config to reflect deployment from attached disks | |
$vm.StorageProfile.OSDisk.Name = $vmName | |
$vm.StorageProfile.OSDisk.CreateOption = "Attach" | |
$vm.StorageProfile.DataDisks | | |
ForEach-Object { $_.CreateOption = "Attach" } | |
$vm.StorageProfile.ImageReference = $null | |
$vm.OSProfile = $null | |
# Re-provision VM from attached disks | |
$vm | | |
New-AzureRmVm ` | |
-ResourceGroupName $rgName ` | |
-Location $location ` | |
-DisableBginfoExtension |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment