Skip to content

Commit f7c025d

Browse files
author
Alexander (Sasha) Nosov
committed
fixed queries and added cleanup
1 parent 90f0f66 commit f7c025d

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

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

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

119119
if ($SubId -like "*.csv") {
120120
$subscriptions = Import-Csv $SubId
121-
}elseif($SubId -ne $null){
121+
}elseif($SubId -ne ""){
122122
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
123123
}else{
124124
$subscriptions = Get-AzSubscription
@@ -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
"
152152

153-
if ($MachineName) {$query += "| where machineName =~ '$($MachineName)'"}
154-
if ($ResourceGroup) {$query += "| where resourceGroup =~ '$($ResourceGroup)'"}
153+
if ($ResourceGroup) {
154+
$query += "| where resourceGroup =~ '$($ResourceGroup)'"
155+
}
156+
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/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)