Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for AzureUSGovernment environment #5

Open
bpweatherill opened this issue Oct 3, 2019 · 0 comments
Open

Fix for AzureUSGovernment environment #5

bpweatherill opened this issue Oct 3, 2019 · 0 comments

Comments

@bpweatherill
Copy link

I'm pasting a script that will work for azure government users that have problems removing the artifacts that the main branch removes for azure commercial. The main changes are the Addition of the -environment variable to the Login line and the removal of the unnecessary host component of the URL in the LinkResourceId variable.


cls

$subscriptionId = Read-Host 'What is your Azure Subscription ID?'
$rgName = Read-Host 'Specify the name of the Resource group in which your VM is located'
$vmName = Read-Host 'Specify the name of the VM'

# Sign-in with Azure account credentials
Login-AzureRmAccount -Environment AzureUSGovernment

# Select Azure Subscription
Select-AzureRmSubscription -SubscriptionId $subscriptionId

# Remove any locks t
$locks = Get-AzureRmResourceLock -ResourceGroupName $rgName -ResourceName $vmName -ResourceType Microsoft.Compute/virtualMachines
if ($locks -ne $null -and $locks.Count -ge 0){
	$canDelete =  Read-Host 'The VM has locks that could prevent cleanup of Azure Site Recovery stale links left from previous protection. Do you want the locks deleted to ensure cleanup goes smoothly? Reply with Y/N.'
	
	if ($canDelete.ToLower() -eq "y"){
		Foreach ($lock in $locks) {
			$lockId = $lock.LockId
			Remove-AzureRmResourceLock -LockId $lockId -Force
			Write-Host "Removed Lock $lockId for $vmName"
		}	
	}
}

$linksResourceId = '/subscriptions/' + $subscriptionId  + '/providers/Microsoft.Resources/links'
$vmId = '/subscriptions/' + $subscriptionId + '/resourceGroups/' + $rgName + '/providers/Microsoft.Compute/virtualMachines/' + $vmName + '/'

Write-Host $("Deleting links for $vmId using resourceId: $linksResourceId")
 

$links = @(Get-AzureRmResource -ResourceId $linksResourceId|  Where-Object {$_.Properties.sourceId -match $vmId -and $_.Properties.targetId.ToLower().Contains("microsoft.recoveryservices/vaults")})
Write-Host "Links to be deleted"
$links

#Delete all links which are of type 
Foreach ($link in $links)

{
 Write-Host $("Deleting link " + $link.Name)
 Remove-AzureRmResource -ResourceId $link.ResourceId -Force
}


$links = @(Get-AzureRmResource -ResourceId $linksResourceId|  Where-Object {$_.Properties.sourceId -match $vmId -and $_.Properties.targetId.ToLower().Contains("/protecteditemarmid/")})
Write-Host "Cross subscription Links to be deleted"
$links


#Delete all links which are of type 
Foreach ($link in $links)

{
 Write-Host $("Deleting link " + $link.Name)
 Remove-AzureRmResource -ResourceId $link.ResourceId -Force
}
 
Write-Host $("Deleted all links ")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant