Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marckean/5e903f6b681ddc0599ebec4773a7d119 to your computer and use it in GitHub Desktop.
Save marckean/5e903f6b681ddc0599ebec4773a7d119 to your computer and use it in GitHub Desktop.
Add-AzureRmAccount
##########################################################################################
########################## Select Source Subscription ############################
##########################################################################################
#region Select a source & target subscription | @marckean
$subscription = Get-AzureRmSubscription | Out-GridView -Title "Select the Source Azure subscription that you want to use ..." -PassThru
$SourceSubscriptionID = $Subscription.SubscriptionId
$SourceSubscriptionName = $Subscription.SubscriptionName
#endregion
##########################################################################################
########################## Select Target Subscription ############################
##########################################################################################
#region Select a source & target subscription | @marckean
$subscription = Get-AzureRmSubscription | Out-GridView -Title "Select the Target Azure subscription that you want to use ..." -PassThru
$TargetSubscriptionID = $Subscription.SubscriptionId
$TargetSubscriptionName = $Subscription.SubscriptionName
#endregion
##########################################################################################
######################### Select Source Managed Disk #############################
##########################################################################################
#region Select Azure Source Managed Disk | @marckean
cls
Select-AzureRmSubscription -SubscriptionName $SourceSubscriptionName
write-host -nonewline "`n`tIn subscription... " -ForegroundColor Yellow; `
write-host -nonewline $SourceSubscriptionName`n -ForegroundColor Green;
write-host -nonewline "`n`tPlease choose a " -ForegroundColor Yellow; `
write-host -nonewline "Managed Disk " -ForegroundColor Green
start-sleep -seconds 1
$ManagedDisks = Get-AzureRmDisk
$ManagedDisks
$ManagedDisk = ($ManagedDisks | select Name, Location, ResourceGroupName, AccountType, DiskSizeGB | Out-GridView -Title "Select a managed disk ..." -PassThru)
$managedDiskName = $ManagedDisk.Name
$sourceResourceGroupName = $ManagedDisk.ResourceGroupName
write-host -nonewline "`n`tYou selected this Managed Disk: " -ForegroundColor Yellow; `
write-host -nonewline $managedDiskName`n -ForegroundColor Green; `
start-sleep -seconds 3
#endregion
##########################################################################################
########################## Target Managed Disk Name ##############################
##########################################################################################
#region Select Azure Source Resource Group | @marckean
cls
write-host -nonewline "`n`tIn subscription... " -ForegroundColor Yellow; `
write-host -nonewline $TargetSubscriptionName`n -ForegroundColor Green;
write-host -nonewline "`n`tPlease enter a " -ForegroundColor Yellow; `
write-host -nonewline "Target " -ForegroundColor Green;
write-host -nonewline "Managed Disk Name`n" -ForegroundColor Yellow
start-sleep -seconds 1
"`n"
$ManagedDisks.Name
write-host -nonewline "`nExisting Managed Disks listed above, enter a name for the new copied Managed Disk: " -ForegroundColor Green
$choice = read-host
$TargetManagedDiskName = $choice -replace "\s",""
write-host -nonewline "`n`tThe new Managed Disk name will be: " -ForegroundColor Yellow; `
write-host -nonewline $TargetManagedDiskName`n -ForegroundColor Green; `
start-sleep -seconds 3
#endregion
##########################################################################################
############################ Target Resource Group ################################
##########################################################################################
#region Target Azure Resource Group - Region and Resource Group creation | @marckean
######################## | Select Azure RM Resource Group | ####################### | @marckean | @marckean
Select-AzureRmSubscription -SubscriptionName $TargetSubscriptionName
cls
Write-Host "`n`tGathering a list of all Azure ARM Resource Groups Azure..." -ForegroundColor Cyan
$TargetResourcegroups = Get-AzureRmResourceGroup | select ResourceGroupName, Location
$TargetResourcegroups | ft
write-host -nonewline "Existing Resource Groups listed above (copy & paste) an existing one or enter a new Resource Group name: " -ForegroundColor Green
$choice = read-host
$TargetResourceGroupName = $choice -replace "\s",""
write-host -nonewline "`n`tThe Resource Group where new managed disk will be moved to: " -ForegroundColor Yellow; `
write-host -nonewline $TargetResourceGroupName`n -ForegroundColor Green; `
start-sleep -seconds 3
####################### | Create the target Resource Group - if it doesn't exist | ####################### | @marckean
cls
Write-Host "`n`tCreating the target Resource group $TargetResourceGroupName (if it don't exist already)..." -ForegroundColor Cyan
if(!(Get-AzureRmResourceGroup -Name $TargetResourceGroupName -ErrorAction SilentlyContinue)){
New-AzureRmResourceGroup -Name $TargetResourceGroupName -Location $ManagedDisk.Location -Force}
#endregion
cls
Write-Host "`n`tThe script is doing the work, grab a coffee..." -ForegroundColor Cyan;
write-host -nonewline "`n`tCheck the target Resource Group shortly for the new copied Managed Disk...`n`n" -ForegroundColor Yellow
##########################################################################################
################### The script does the work, grab a coffee ########################
##########################################################################################
Select-AzureRmSubscription -SubscriptionName $SourceSubscriptionName
#Get the source managed disk
$managedDisk = Get-AzureRMDisk -ResourceGroupName $sourceResourceGroupName -DiskName $managedDiskName
#Set the context to the subscription Id where managed disk will be copied to
#If snapshot is copied to the same subscription then you can skip this step
Select-AzureRmSubscription -SubscriptionId $TargetSubscriptionID
$diskConfig = New-AzureRmDiskConfig -SourceResourceId $managedDisk.Id -Location $managedDisk.Location -CreateOption Copy
#Create a new managed disk in the target subscription and resource group
New-AzureRmDisk -Disk $diskConfig -DiskName $TargetManagedDiskName -ResourceGroupName $targetResourceGroupName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment