Skip to content

Instantly share code, notes, and snippets.

@marckean
Created May 18, 2017 23:06
Show Gist options
  • Save marckean/682397d307bfdc3eb1f17fc14bf4cd66 to your computer and use it in GitHub Desktop.
Save marckean/682397d307bfdc3eb1f17fc14bf4cd66 to your computer and use it in GitHub Desktop.
$StackAadTenantId="<>.onmicrosoft.com"
#region Source Environment | @marckean
$MigrationOptions = @("Azure Public", "Azure Stack")
cls
Write-Host "`nWhere does the VM exist?" -ForegroundColor Cyan
$SourceEnvironment = ($MigrationOptions | Out-GridView -Title "Where are you migrating from?" -PassThru)
#endregion
#region Authentication | @marckean
if($SourceEnvironment -eq 'Azure Stack'){
# Configure the environment with the Add-AzureRmEnvironment cmdlet
Add-AzureRmEnvironment -Name 'AzureStack' `
-ActiveDirectoryEndpoint "https://login.windows.net/$StackAadTenantId/" `
-ActiveDirectoryServiceEndpointResourceId "https://azurestack.local-api/"`
-ResourceManagerEndpoint "https://api.azurestack.local/" `
-GalleryEndpoint "https://gallery.azurestack.local/" `
-GraphEndpoint "https://graph.windows.net/"
# Authenticate a user to Azure Stack (you will be prompted during authentication)
cls
Write-Host "`nEnter credentials for the SOURCE Azure Stack subscription.`n" -ForegroundColor Cyan
$SourceAzure = Get-AzureRmEnvironment 'AzureStack'
$SourceEnv = Login-AzureRmAccount -Environment $SourceAzure -Verbose
}
else {
# Authenticate a user to Azure Public (you will be prompted during authentication)
cls
Write-Host "`nEnter credentials for the SOURCE Azure Public subscription.`n" -ForegroundColor Cyan
$SourceAzure = Get-AzureRmEnvironment 'AzureCloud'
$SourceEnv = Login-AzureRmAccount -Environment $SourceAzure -Verbose
}
#endregion
#region Select a source subscription | @marckean
Select-AzureRmProfile -Profile $SourceEnv
$SourceSubscription = (Get-AzureRmSubscription | Out-GridView -Title "Choose a Source Subscription ..." -PassThru)
$SourceSubscriptionID = $SourceSubscription.SubscriptionId
$SourceSubscriptionName = $SourceSubscription.SubscriptionName
Select-AzureRmProfile -Profile $SourceEnv; Get-AzureRmSubscription -SubscriptionName $SourceSubscriptionName | Select-AzureRmSubscription
#endregion
#region Select Azure Source Resource Group | @marckean
cls
write-host -nonewline "`n`tIn... " -ForegroundColor Yellow; `
write-host -nonewline $SourceEnvironment`n -ForegroundColor Green; `
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 "Source " -ForegroundColor Green;
write-host -nonewline "Resource Group" -ForegroundColor Yellow
start-sleep -seconds 1
$SourceResourceGroups = Find-AzureRmResourceGroup | select Name, Location
$SourceResourceGroups | ft
$SourceResourceGroup = ($SourceResourceGroups | Out-GridView -Title "Select the source ARM Resource Group ..." -PassThru)
$SourceResourceGroupName = $SourceResourceGroup.Name
write-host -nonewline "`n`tThis will be the source Resource Group: " -ForegroundColor Yellow; `
write-host -nonewline $SourceResourceGroupName`n -ForegroundColor Green; `
start-sleep -seconds 3
#endregion
#region Select Azure Source Virtual Machine | @marckean
cls
write-host -nonewline "`n`tIn... " -ForegroundColor Yellow; `
write-host -nonewline $SourceEnvironment`n -ForegroundColor Green; `
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 "Source " -ForegroundColor Green;
write-host -nonewline "Virtual Machine" -ForegroundColor Yellow
start-sleep -seconds 1
$SourceVirtualMachine = Get-AzureRmVM -ResourceGroupName $SourceResourceGroupName | select Name
$SourceVirtualMachine | ft
$SourceVirtualMachine = ($SourceVirtualMachine | Out-GridView -Title "Select the source virtual machine ..." -PassThru)
$SourceVMJSON = Get-AzureRmVM -ResourceGroupName $SourceResourceGroupName -Name $SourceVirtualMachine.Name
write-host -nonewline "`n`tThis will be the source Virtual Machine: " -ForegroundColor Yellow; `
write-host -nonewline $SourceVirtualMachine.Name`n -ForegroundColor Green; `
start-sleep -seconds 3
#endregion
$vmname = $SourceVMJSON.Name
$InterfaceIDs = $SourceVMJSON.NetworkInterfaceIDs
#region Azure Resource Removal Process - Q & A
foreach($InterfaceID in $InterfaceIDs){
# Identification
$VM = Get-AzureRmVM -ResourceGroupName $SourceResourceGroupName -Name $vmname
$NIC = Get-AzureRmNetworkInterface -ResourceGroupName $SourceResourceGroupName | ? {$_.IpConfigurations.Id -match $InterfaceID}
$NSG = (Get-AzureRmNetworkSecurityGroup -ResourceGroupName $SourceResourceGroupName) | ? {$_.NetworkInterfaces.Id -match $NIC.Id}
$PIP = Get-AzureRmPublicIpAddress -ResourceGroupName $SourceResourceGroupName | ? {$_.IpConfiguration.Id -match $InterfaceID}
$AS = Get-AzureRmAvailabilitySet -ResourceGroupName $SourceResourceGroupName | ? {$_.VirtualMachinesReferences.id -match $VM.id}
$LBs = Get-AzureRmLoadBalancer -ResourceGroupName $SourceResourceGroupName
$vNets = Get-AzureRmVirtualNetwork -ResourceGroupName $SourceResourceGroupName
$Storages = Get-AzureRmStorageAccount -ResourceGroupName $SourceResourceGroupName
# Remove VM
cls
$RemoveVM = [System.Windows.Forms.MessageBox]::Show("Do you want to remove the VM: $vmname`?","VM Resource Removal Check","YesNo","Information")
if($RemoveVM -eq "Yes"){
write-host -nonewline "`n`tRemoving Virtual Machine: " -ForegroundColor Yellow; `
write-host -nonewline $vmname`n -ForegroundColor Green
Remove-AzureRmVM -ResourceGroupName $SourceResourceGroupName -Name $vmname -Force
# Remove NIC
cls
$RemoveNIC = [System.Windows.Forms.MessageBox]::Show("Do you want to remove the NIC: " + $NIC.Name + "?","NIC Resource Removal Check","YesNo","Information")
if($RemoveNIC -eq "Yes"){
write-host -nonewline "`n`tRemoving NIC: " -ForegroundColor Yellow; `
write-host -nonewline $NIC.Name`n -ForegroundColor Green
#$NICResource = Get-AzureRmResource -ResourceGroupName $SourceResourceGroupName -ResourceName $NIC.Name -ExpandProperties
# Flush the resource dependency
# $NICResource.Properties.virtualMachine = $null
#Set-AzureRmResource -ResourceId $NIC.id -Properties $NICResource.Properties -Force
Remove-AzureRmNetworkInterface -ResourceGroupName $SourceResourceGroupName -Name $NIC.Name -Force
# Remove PIP
cls
$RemovePIP = [System.Windows.Forms.MessageBox]::Show("Do you want to remove the PIP: " + $PIP.Name + "?","PIP Resource Removal Check","YesNo","Information")
if($RemovePIP -eq "Yes"){
write-host -nonewline "`n`tRemoving PIP: " -ForegroundColor Yellow; `
write-host -nonewline $PIP.Name`n -ForegroundColor Green
Remove-AzureRmPublicIpAddress -ResourceGroupName $SourceResourceGroupName -Name $PIP.Name -Force
# Remove NSG
cls
$RemoveNSG = [System.Windows.Forms.MessageBox]::Show("Do you want to remove the NSG: " + $NSG.Name + "?","NSG Resource Removal Check","YesNo","Information")
if($RemoveNSG -eq "Yes"){
write-host -nonewline "`n`tRemoving NSG: " -ForegroundColor Yellow; `
write-host -nonewline $NSG.Name`n -ForegroundColor Green
Remove-AzureRmNetworkSecurityGroup -ResourceGroupName $SourceResourceGroupName -Name $NSG.Name -Force
# Remove AS
cls
$RemoveAS = [System.Windows.Forms.MessageBox]::Show("Do you want to remove the AS: " + $AS.Name + "?","AS Resource Removal Check","YesNo","Information")
if($RemoveAS -eq "Yes"){
write-host -nonewline "`n`tRemoving AS: " -ForegroundColor Yellow; `
write-host -nonewline $AS.Name`n -ForegroundColor Green
Remove-AzureRmAvailabilitySet -ResourceGroupName $SourceResourceGroupName -Name $AS.Name -Force
# Remove LBs & their PIPs
cls
foreach($LB in $LBs){
$RemoveLB = [System.Windows.Forms.MessageBox]::Show("Do you want to remove the LB along with it's PIP: " + $LB.Name + "?","LB Resource Removal Check","YesNo","Information")
if($RemoveLB -eq "Yes"){
$FE = ($LB).FrontendIpConfigurations.id
$LBPIP = Get-AzureRmPublicIpAddress -ResourceGroupName $SourceResourceGroupName | ? {$_.IpConfiguration.Id -eq $FE}
write-host -nonewline "`n`tRemoving LB: " -ForegroundColor Yellow; `
write-host -nonewline $LB.Name`n -ForegroundColor Green
Remove-AzureRmLoadBalancer -ResourceGroupName $SourceResourceGroupName -Name $LB.Name -Force
write-host -nonewline "`n`tRemoving LB's PIP: " -ForegroundColor Yellow; `
write-host -nonewline $LBPIP.Name`n -ForegroundColor Green
Remove-AzureRmPublicIpAddress -ResourceGroupName $SourceResourceGroupName -Name $LBPIP.Name -Force
}
# Remove LBs & their PIPs
cls
foreach($vNet in $vNets){
$RemovevNet = [System.Windows.Forms.MessageBox]::Show("Do you want to remove the vNet: " + $vNet.Name + "?","vNet Resource Removal Check","YesNo","Information")
if($RemovevNet -eq "Yes"){
write-host -nonewline "`n`tRemoving vNet: " -ForegroundColor Yellow; `
write-host -nonewline $vNet.Name`n -ForegroundColor Green
Remove-AzureRmVirtualNetwork -ResourceGroupName $SourceResourceGroupName -Name $vNet.Name -Force
}
# Remove Storage
cls
foreach($Storage in $Storages){
$RemoveStorage = [System.Windows.Forms.MessageBox]::Show("Do you want to remove the Storage: " + $Storage.StorageAccountName + "?","Storage Resource Removal Check","YesNo","Information")
if($RemoveStorage -eq "Yes"){
write-host -nonewline "`n`tRemoving Storage: " -ForegroundColor Yellow; `
write-host -nonewline $Storage.StorageAccountName`n -ForegroundColor Green
Remove-AzureRmStorageAccount -ResourceGroupName $SourceResourceGroupName -Name $Storage.StorageAccountName
}
}
}
}
}
}
}
}
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment