Skip to content

Commit caec665

Browse files
authored
Merge pull request #1141 from anosov1960/master
added uninstall script for SQL azure extension.
2 parents 0d515d1 + 90f0f66 commit caec665

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
services: Azure Arc-enabled SQL Server
3+
platforms: Azure
4+
author: anosov1960
5+
ms.author: sashan
6+
ms.date: 4/6/2023
7+
---
8+
9+
10+
# Overview
11+
12+
This script provides a scalable solution to uninstall Azure extension for SQL Server on a specific Arc-enabled server,
13+
all Arc-enabled servers in a specific resource group, specific subscription, a list of subscriptions or the entire account.
14+
You can specify a single subscription to scan, or provide a list of subscriptions as a .CSV file.
15+
16+
# Prerequisites
17+
18+
- You must have at least a *Contributor* role in each subscription you modify.
19+
- The Azure extension for SQL Server is updated to version 1.1.2230.58 or newer.
20+
21+
# Launching the script
22+
23+
The script accepts the following command line parameters:
24+
25+
| **Parameter**                                         | **Value**                                                                       | **Description** |
26+
|:--|:--|:--|
27+
|-SubId|subscription_id *or* a file_name|Optional: subscription id or a .csv file with the list of subscriptions<sup>1</sup>. |
28+
|-ResourceGroup |resource_group_name|Optional: Limit the scope to a specific resource group|
29+
|-MachineName |machine_name|Optional: Limit the scope to a specific machine|
30+
|-All|\$True or \$False (default)|Optional. Uninstall Azure extension on all Arc-enabled servers in all subscriptions you have contributor access to.|
31+
32+
<sup>1</sup>You can create a .csv file using the following command and then edit to remove the subscriptions you don't want to scan.
33+
```PowerShell
34+
Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation
35+
```
36+
37+
## Example 1
38+
39+
The following command will scan all the subscriptions to which the user has contributor access to, and uninstall Azure extension for SQL Server on all Arc-enabled servers where it is installed.
40+
41+
```PowerShell
42+
.\uninstall-azure-extension-for-sql-server.ps1 -All $True
43+
```
44+
45+
## Example 2
46+
47+
The following command will scan the subscription `<sub_id>` and uninstall Azure extension for SQL Server on all Arc-enabled servers where it is installed.
48+
49+
```PowerShell
50+
.\uninstall-azure-extension-for-sql-server.ps1 -SubId <sub_id>
51+
```
52+
53+
## Example 3
54+
55+
The following command will scan resource group <resource_group_name> in the subscription `<sub_id>` and and uninstall Azure extension for SQL Server on all Arc-enabled servers where it is installed.
56+
57+
```PowerShell
58+
.\uninstall-azure-extension-for-sql-server.ps1 -SubId <sub_id> -ResourceGroup <resource_group_name>
59+
```
60+
61+
# Running the script using Cloud Shell
62+
63+
This option is recommended because Cloud shell has the Azure PowerShell modules pre-installed and you are automatically authenticated. Use the following steps to run the script in Cloud Shell.
64+
65+
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
66+
67+
2. Upload the script to your cloud shell using the following command:
68+
69+
```console
70+
curl https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 -o uninstall-azure-extension-for-sql-server.ps1
71+
```
72+
73+
3. Run the script.
74+
75+
```console
76+
.\uninstall-azure-extension-for-sql-server.ps1 -All $True
77+
```
78+
79+
> [!NOTE]
80+
> - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS.
81+
> - The script will be uploaded directly to the home folder associated with your Cloud Shell session.
82+
83+
# Running the script from a PC
84+
85+
86+
Use the following steps to run the script in a PowerShell session on your PC.
87+
88+
1. Copy the script to your current folder:
89+
90+
```console
91+
curl https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 -o uninstall-azure-extension-for-sql-server.ps1
92+
```
93+
94+
1. Make sure the NuGet package provider is installed:
95+
96+
```console
97+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
98+
Install-packageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Scope CurrentUser -Force
99+
```
100+
101+
1. Make sure the the Az module is installed. For more information, see [Install the Azure Az PowerShell module](https://learn.microsoft.com/powershell/azure/install-az-ps):
102+
103+
```console
104+
Install-Module Az -Scope CurrentUser -Repository PSGallery -Force
105+
```
106+
107+
1. Connect to Azure with an authenticated account using an authentication method of your choice. For more information, see [Connect-AzAccount](https://learn.microsoft.com/powershell/module/az.accounts/connect-azaccount).
108+
109+
```console
110+
Connect-AzAccount <parameters>
111+
```
112+
113+
1. Run the script using the desired scope.
114+
115+
```console
116+
.\uninstall-azure-extension-for-sql-server.ps1 -All $True
117+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#
2+
# This script provides a scaleable solution to uninstall Azure extension for SQL Server on a specific Arc-enabled server,
3+
# all Arc-enabled servers in a specific resource group, specific subscription, a list of subscriptions or the entire account.
4+
#
5+
# You can specfy a single subscription to scan, or provide subscriptions as a .CSV file with the list of IDs.
6+
# If not specified, all subscriptions your role has access to are scanned.
7+
#
8+
# The script accepts the following command line parameters:
9+
#
10+
# -SubId [subscription_id] | [csv_file_name] (Limit scope to specific subscriptions. Accepts a .csv file with the list of subscriptions.
11+
# If not specified all subscriptions will be scanned)
12+
# -ResourceGroup [resource_goup] (Limit scope to a specific resoure group)
13+
# -MachineName [machine_name] (Limit scope to a specific machine)
14+
# -All (Uninstall Azure extension on all Arc-enabled servers in all subscriptions you have contributor access to).
15+
#
16+
#
17+
18+
param (
19+
[Parameter (Mandatory=$false)]
20+
[string] $SubId,
21+
[Parameter (Mandatory= $false)]
22+
[string] $ResourceGroup,
23+
[Parameter (Mandatory= $false)]
24+
[string] $MachineName,
25+
[Parameter (Mandatory= $false)]
26+
[boolean] $All=$false
27+
)
28+
29+
function CheckModule ($m) {
30+
31+
# This function ensures that the specified module is imported into the session
32+
# If module is already imported - do nothing
33+
34+
if (!(Get-Module | Where-Object {$_.Name -eq $m})) {
35+
# If module is not imported, but available on disk then import
36+
if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
37+
Import-Module $m
38+
}
39+
else {
40+
41+
# If module is not imported, not available on disk, but is in online gallery then install and import
42+
if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
43+
Install-Module -Name $m -Force -Verbose -Scope CurrentUser
44+
Import-Module $m
45+
}
46+
else {
47+
48+
# If module is not imported, not available and not in online gallery then abort
49+
write-host "Module $m not imported, not available and not in online gallery, exiting."
50+
EXIT 1
51+
}
52+
}
53+
}
54+
}
55+
56+
#
57+
# Suppress warnings
58+
#
59+
Update-AzConfig -DisplayBreakingChangeWarning $false
60+
61+
# Load required modules
62+
$requiredModules = @(
63+
"Az.Accounts",
64+
"Az.ConnectedMachine",
65+
"Az.ResourceGraph"
66+
)
67+
$requiredModules | Foreach-Object {CheckModule $_}
68+
69+
# Subscriptions to scan
70+
71+
if ($SubId -like "*.csv") {
72+
$subscriptions = Import-Csv $SubId
73+
}elseif($SubId -ne $null){
74+
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
75+
}elseif($All){
76+
$subscriptions = Get-AzSubscription
77+
}else {
78+
Write-Host ([Environment]::NewLine + "-- Parameter missing --")
79+
exit
80+
}
81+
82+
Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --")
83+
84+
# Scan arc-enabled servers in each subscription
85+
86+
foreach ($sub in $subscriptions){
87+
88+
if ($sub.State -ne "Enabled") {continue}
89+
90+
try {
91+
Set-AzContext -SubscriptionId $sub.Id
92+
}catch {
93+
write-host "[Environment]::NewLine + Invalid subscription: $($sub.Id)"
94+
{continue}
95+
}
96+
97+
$query = "
98+
resources
99+
| where type =~ 'microsoft.hybridcompute/machines/extensions'
100+
| extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type), provisioningState = tostring(properties.provisioningState)
101+
| where extensionPublisher =~ 'Microsoft.AzureData'
102+
| where provisioningState =~ 'Succeeded'
103+
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
104+
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
105+
"
106+
107+
if ($MachineName) {$query += "| where machineName =~ '$($MachineName)'"}
108+
if ($ResourceGroup) {$query += "| where resourceGroup =~ '$($ResourceGroup)'"}
109+
110+
$resources = Search-AzGraph -Query "$($query) | where subscriptionId =~ '$($sub.Id)'"
111+
foreach ($r in $resources) {
112+
113+
114+
115+
$setID = @{
116+
MachineName = $r.MachineName
117+
ResourceGroup = $r.resourceGroup
118+
Name = $r.extensionName
119+
}
120+
121+
Remove-AzConnectedMachineExtension @SetId -NoWait # | Out-Null
122+
123+
}
124+
}
125+
126+

0 commit comments

Comments
 (0)