Skip to content

Commit ec8b042

Browse files
authored
Merge pull request #1142 from anosov1960/master
Added cleanup, fixed readme etc.
2 parents caec665 + f950653 commit ec8b042

File tree

4 files changed

+104
-46
lines changed

4 files changed

+104
-46
lines changed

samples/manage/azure-arc-enabled-sql-server/modify-license-type/README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ If not specified, all subscriptions your role has access to are scanned.
1919

2020
- You must have at least a *Contributor* role in each subscription you modify.
2121
- The Azure extension for SQL Server is updated to version 1.1.2230.58 or newer.
22+
- You must be connected to Azure AD and logged in to your Azure account. If your account have access to multiple tenants, make sure to log in with a specific tenant ID.
23+
2224

2325
# Launching the script
2426

@@ -67,13 +69,19 @@ This option is recommended because Cloud shell has the Azure PowerShell modules
6769

6870
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
6971

70-
2. Upload the script to your cloud shell using the following command:
72+
1. Connect to Azure AD
73+
74+
```console
75+
Connect-AzureAD
76+
```
77+
78+
1. Upload the script to your cloud shell using the following command:
7179

7280
```console
7381
curl https://raw.githubusercontent.com/microsoft/sql-server-samples/master/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 -o modify-license-type.ps1
7482
```
7583

76-
3. Run the script.
84+
1. Run the script.
7785

7886
```console
7987
.//modify-license-type.ps1 -LicenseType Paid
@@ -107,10 +115,11 @@ Use the following steps to run the script in a PowerShell session on your PC.
107115
Install-Module Az -Scope CurrentUser -Repository PSGallery -Force
108116
```
109117

110-
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).
118+
1. Connect to Azure AD and log in to your Azure account.
111119

112120
```console
113-
Connect-AzAccount <parameters>
121+
Connect-AzureAD
122+
Connect-AzAccount -TenantID (Get-AzureADTenantDetail).ObjectId
114123
```
115124

116125
1. Run the script using the desired scope.

samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ $requiredModules | Foreach-Object {CheckModule $_}
118118

119119
if ($SubId -like "*.csv") {
120120
$subscriptions = Import-Csv $SubId
121-
}elseif($SubId -ne $null){
122-
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
121+
}elseif($SubId -ne ""){
122+
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
123123
}else{
124124
$subscriptions = Get-AzSubscription
125125
}
@@ -143,17 +143,26 @@ foreach ($sub in $subscriptions){
143143
$query = "
144144
resources
145145
| where type =~ 'microsoft.hybridcompute/machines/extensions'
146+
| where subscriptionId =~ '$($sub.Id)'
146147
| extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type), provisioningState = tostring(properties.provisioningState)
148+
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
147149
| where extensionPublisher =~ 'Microsoft.AzureData'
148150
| where provisioningState =~ 'Succeeded'
149-
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
150-
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
151151
"
152+
153+
if ($ResourceGroup) {
154+
$query += "| where resourceGroup =~ '$($ResourceGroup)'"
155+
}
152156

153-
if ($MachineName) {$query += "| where machineName =~ '$($MachineName)'"}
154-
if ($ResourceGroup) {$query += "| where resourceGroup =~ '$($ResourceGroup)'"}
157+
if ($MachineName) {
158+
$query += "| where machineName =~ '$($MachineName)'"
159+
}
160+
161+
$query += "
162+
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
163+
"
155164

156-
$resources = Search-AzGraph -Query "$($query) | where subscriptionId =~ '$($sub.Id)'"
165+
$resources = Search-AzGraph -Query "$($query)"
157166
foreach ($r in $resources) {
158167

159168
$setID = @{

samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ You can specify a single subscription to scan, or provide a list of subscription
1717

1818
- You must have at least a *Contributor* role in each subscription you modify.
1919
- The Azure extension for SQL Server is updated to version 1.1.2230.58 or newer.
20+
- You must be connected to Azure AD and logged in to your Azure account. If your account have access to multiple tenants, make sure to log in with a specific tenant ID.
2021

21-
# Launching the script
22+
# Launching the script
2223

2324
The script accepts the following command line parameters:
2425

@@ -39,7 +40,7 @@ Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation
3940
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.
4041

4142
```PowerShell
42-
.\uninstall-azure-extension-for-sql-server.ps1 -All $True
43+
.\uninstall-azure-extension-for-sql-server.ps1 -SubId ALL
4344
```
4445

4546
## Example 2
@@ -57,23 +58,36 @@ The following command will scan resource group <resource_group_name> in the subs
5758
```PowerShell
5859
.\uninstall-azure-extension-for-sql-server.ps1 -SubId <sub_id> -ResourceGroup <resource_group_name>
5960
```
61+
## Example 4
62+
63+
The following command will uninstall Azure extension for SQL Server on a specific Arc-enabled server.
64+
65+
```PowerShell
66+
.\uninstall-azure-extension-for-sql-server.ps1 -SubId <sub_id> -MachineName <machine_name>
67+
```
6068

6169
# Running the script using Cloud Shell
6270

6371
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.
6472

6573
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
6674

67-
2. Upload the script to your cloud shell using the following command:
75+
1. Connect to Azure AD
76+
77+
```console
78+
Connect-AzureAD
79+
```
80+
81+
1. Upload the script to your cloud shell using the following command:
6882

6983
```console
7084
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
7185
```
7286

73-
3. Run the script.
87+
1. Run the script.
7488

7589
```console
76-
.\uninstall-azure-extension-for-sql-server.ps1 -All $True
90+
.\uninstall-azure-extension-for-sql-server.ps1 -SubId ALL
7791
```
7892

7993
> [!NOTE]
@@ -82,7 +96,6 @@ This option is recommended because Cloud shell has the Azure PowerShell modules
8296

8397
# Running the script from a PC
8498

85-
8699
Use the following steps to run the script in a PowerShell session on your PC.
87100

88101
1. Copy the script to your current folder:
@@ -104,10 +117,11 @@ Use the following steps to run the script in a PowerShell session on your PC.
104117
Install-Module Az -Scope CurrentUser -Repository PSGallery -Force
105118
```
106119

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).
120+
1. Connect to Azure AD and login to your Azure account.
108121

109122
```console
110-
Connect-AzAccount <parameters>
123+
Connect-AzureAD
124+
Connect-AzAccount -TenantID (Get-AzureADTenantDetail).ObjectId
111125
```
112126

113127
1. Run the script using the desired scope.

samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@
77
#
88
# The script accepts the following command line parameters:
99
#
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).
10+
# -SubId [subscription_id] | [csv_file_name] | ALL (Limit scope to specific subscriptions. Accepts a .csv file with the list of subscriptions.
11+
# If ALL is 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)
1514
#
1615
#
1716

1817
param (
19-
[Parameter (Mandatory=$false)]
18+
[Parameter (Mandatory=$true)]
2019
[string] $SubId,
2120
[Parameter (Mandatory= $false)]
2221
[string] $ResourceGroup,
23-
[Parameter (Mandatory= $false)]
24-
[string] $MachineName,
25-
[Parameter (Mandatory= $false)]
26-
[boolean] $All=$false
27-
)
22+
[Parameter (Mandatory= $true)]
23+
[string] $MachineName
24+
)
2825

2926
function CheckModule ($m) {
3027

@@ -60,6 +57,7 @@ Update-AzConfig -DisplayBreakingChangeWarning $false
6057

6158
# Load required modules
6259
$requiredModules = @(
60+
"AzureAD",
6361
"Az.Accounts",
6462
"Az.ConnectedMachine",
6563
"Az.ResourceGraph"
@@ -70,13 +68,13 @@ $requiredModules | Foreach-Object {CheckModule $_}
7068

7169
if ($SubId -like "*.csv") {
7270
$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
71+
}elseif($SubId -like "ALL"){
72+
$subscriptions = Get-AzSubscription -TenantID (Get-AzureADTenantDetail).ObjectId
73+
}elseif($SubId -ne ""){
74+
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription -TenantID (Get-AzureADTenantDetail).ObjectId
75+
}else{
76+
Write-Host ([Environment]::NewLine + "-- Subscription parameter is missing --")
77+
exit
8078
}
8179

8280
Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --")
@@ -94,33 +92,61 @@ foreach ($sub in $subscriptions){
9492
{continue}
9593
}
9694

97-
$query = "
95+
$query1 = "
9896
resources
97+
| where subscriptionId =~ '$($sub.Id)'
9998
| where type =~ 'microsoft.hybridcompute/machines/extensions'
10099
| extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type), provisioningState = tostring(properties.provisioningState)
100+
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
101101
| where extensionPublisher =~ 'Microsoft.AzureData'
102102
| where provisioningState =~ 'Succeeded'
103-
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
103+
"
104+
$query2 = "
105+
resources
106+
| where subscriptionId =~ '$($sub.Id)'
107+
| where type =~ 'Microsoft.AzureArcData/SqlServerInstances'
108+
| where properties.status =~ 'Connected'
109+
"
110+
111+
if ($ResourceGroup) {
112+
$query1 += "| where resourceGroup =~ '$($ResourceGroup)'"
113+
$query2 += "| where resourceGroup =~ '$($ResourceGroup)'"
114+
}
115+
116+
if ($MachineName) {
117+
$query1 += "| where machineName =~ '$($MachineName)'"
118+
$query2 += "| where name =~ '$($MachineName)'"
119+
}
120+
121+
$query1 += "
104122
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
105123
"
106124

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)'"
125+
#
126+
# Delete Azure extension for SQL
127+
$resources = Search-AzGraph -Query "$($query1)"
111128
foreach ($r in $resources) {
112129

113-
114-
115130
$setID = @{
116131
MachineName = $r.MachineName
117132
ResourceGroup = $r.resourceGroup
118133
Name = $r.extensionName
119134
}
120135

121-
Remove-AzConnectedMachineExtension @SetId -NoWait # | Out-Null
136+
Remove-AzConnectedMachineExtension @SetId -NoWait | Out-Null
137+
138+
}
139+
140+
#
141+
# Remove Arc-enable SQL Server resources
142+
$resources = Search-AzGraph -Query "$($query2)"
143+
144+
foreach ($r in $resources) {
145+
146+
Remove-AzResource -ResourceId $r.resourceId -Force | Out-Null
122147

123148
}
149+
124150
}
125151

126152

0 commit comments

Comments
 (0)