Skip to content

Instantly share code, notes, and snippets.

@averkinderen
Created September 4, 2017 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save averkinderen/5e9b560b94544e451e9e4cf2481f8e6e to your computer and use it in GitHub Desktop.
Save averkinderen/5e9b560b94544e451e9e4cf2481f8e6e to your computer and use it in GitHub Desktop.
Capture Azure VM and create Image
#################################################
# Author: Alexandre Verkinderen
# Company: cubesys
# date: 04/09/2017
################################################
Login-AzureRmAccount
#region variables
#select resource Group
$rgName = ( Get-AzureRmResourceGroup | Out-GridView -Title "Select an Azure Resource Group ...and press Enter" -PassThru).ResourceGroupName
#Select Virtual Machine
$vmName = ( Get-AzureRmvm -ResourceGroupName $rgName | Out-GridView -Title "Select a Virtual machine ...and press Enter" -PassThru).Name
#Select Location
$location = (Get-AzureRmLocation | Out-GridView -Title "Select a Location...and press Enter" -PassThru).Location
$imageName = Read-Host -Prompt 'Provide Image Name'
$caption = "Confirm"
$message = "Are you sure you want to create an image of server " + $vmName + " from resource group " + $rgName + " in location " + $location
$yes = new-Object System.Management.Automation.Host.ChoiceDescription "&Yes","help"
$no = new-Object System.Management.Automation.Host.ChoiceDescription "&No","help"
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
#endregion
$answer = $host.ui.PromptForChoice($caption,$message,$choices,0)
switch ($answer){
0 {
# Make sure the VM has been deallocated.
#Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Force
#Set the status of the virtual machine to Generalized and get the vm
Set-AzureRmVm -ResourceGroupName $rgName -Name $vmName -Generalized
$vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $rgName
#Create the image configuration.
$image = New-AzureRmImageConfig -Location $location -SourceVirtualMachineId $vm.ID
#Create the image
New-AzureRmImage -Image $image -ImageName $imageName -ResourceGroupName $rgName
}
1 {"You entered no"; break}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment