Merge pull request #1142 from anosov1960/master

Added cleanup, fixed readme etc.
This commit is contained in:
Alexander (Sasha) Nosov
2023-04-07 13:14:43 -10:00
committed by GitHub
4 changed files with 104 additions and 46 deletions
@@ -19,6 +19,8 @@ If not specified, all subscriptions your role has access to are scanned.
- You must have at least a *Contributor* role in each subscription you modify.
- The Azure extension for SQL Server is updated to version 1.1.2230.58 or newer.
- 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.
# Launching the script
@@ -67,13 +69,19 @@ This option is recommended because Cloud shell has the Azure PowerShell modules
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
2. Upload the script to your cloud shell using the following command:
1. Connect to Azure AD
```console
Connect-AzureAD
```
1. Upload the script to your cloud shell using the following command:
```console
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
```
3. Run the script.
1. Run the script.
```console
.//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.
Install-Module Az -Scope CurrentUser -Repository PSGallery -Force
```
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).
1. Connect to Azure AD and log in to your Azure account.
```console
Connect-AzAccount <parameters>
Connect-AzureAD
Connect-AzAccount -TenantID (Get-AzureADTenantDetail).ObjectId
```
1. Run the script using the desired scope.
@@ -118,8 +118,8 @@ $requiredModules | Foreach-Object {CheckModule $_}
if ($SubId -like "*.csv") {
$subscriptions = Import-Csv $SubId
}elseif($SubId -ne $null){
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
}elseif($SubId -ne ""){
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
}else{
$subscriptions = Get-AzSubscription
}
@@ -143,17 +143,26 @@ foreach ($sub in $subscriptions){
$query = "
resources
| where type =~ 'microsoft.hybridcompute/machines/extensions'
| where subscriptionId =~ '$($sub.Id)'
| extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type), provisioningState = tostring(properties.provisioningState)
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
| where extensionPublisher =~ 'Microsoft.AzureData'
| where provisioningState =~ 'Succeeded'
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
"
if ($ResourceGroup) {
$query += "| where resourceGroup =~ '$($ResourceGroup)'"
}
if ($MachineName) {
$query += "| where machineName =~ '$($MachineName)'"
}
$query += "
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
"
if ($MachineName) {$query += "| where machineName =~ '$($MachineName)'"}
if ($ResourceGroup) {$query += "| where resourceGroup =~ '$($ResourceGroup)'"}
$resources = Search-AzGraph -Query "$($query) | where subscriptionId =~ '$($sub.Id)'"
$resources = Search-AzGraph -Query "$($query)"
foreach ($r in $resources) {
$setID = @{
@@ -17,8 +17,9 @@ You can specify a single subscription to scan, or provide a list of subscription
- You must have at least a *Contributor* role in each subscription you modify.
- The Azure extension for SQL Server is updated to version 1.1.2230.58 or newer.
- 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.
# Launching the script
# Launching the script
The script accepts the following command line parameters:
@@ -39,7 +40,7 @@ Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation
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.
```PowerShell
.\uninstall-azure-extension-for-sql-server.ps1 -All $True
.\uninstall-azure-extension-for-sql-server.ps1 -SubId ALL
```
## Example 2
@@ -57,6 +58,13 @@ The following command will scan resource group <resource_group_name> in the subs
```PowerShell
.\uninstall-azure-extension-for-sql-server.ps1 -SubId <sub_id> -ResourceGroup <resource_group_name>
```
## Example 4
The following command will uninstall Azure extension for SQL Server on a specific Arc-enabled server.
```PowerShell
.\uninstall-azure-extension-for-sql-server.ps1 -SubId <sub_id> -MachineName <machine_name>
```
# Running the script using Cloud Shell
@@ -64,16 +72,22 @@ This option is recommended because Cloud shell has the Azure PowerShell modules
1. Launch the [Cloud Shell](https://shell.azure.com/). For details, [read more about PowerShell in Cloud Shell](https://aka.ms/pscloudshell/docs).
2. Upload the script to your cloud shell using the following command:
1. Connect to Azure AD
```console
Connect-AzureAD
```
1. Upload the script to your cloud shell using the following command:
```console
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
```
3. Run the script.
1. Run the script.
```console
.\uninstall-azure-extension-for-sql-server.ps1 -All $True
.\uninstall-azure-extension-for-sql-server.ps1 -SubId ALL
```
> [!NOTE]
@@ -82,7 +96,6 @@ This option is recommended because Cloud shell has the Azure PowerShell modules
# Running the script from a PC
Use the following steps to run the script in a PowerShell session on your PC.
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.
Install-Module Az -Scope CurrentUser -Repository PSGallery -Force
```
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).
1. Connect to Azure AD and login to your Azure account.
```console
Connect-AzAccount <parameters>
Connect-AzureAD
Connect-AzAccount -TenantID (Get-AzureADTenantDetail).ObjectId
```
1. Run the script using the desired scope.
@@ -7,24 +7,21 @@
#
# The script accepts the following command line parameters:
#
# -SubId [subscription_id] | [csv_file_name] (Limit scope to specific subscriptions. Accepts a .csv file with the list of subscriptions.
# If not specified all subscriptions will be scanned)
# -ResourceGroup [resource_goup] (Limit scope to a specific resoure group)
# -MachineName [machine_name] (Limit scope to a specific machine)
# -All (Uninstall Azure extension on all Arc-enabled servers in all subscriptions you have contributor access to).
# -SubId [subscription_id] | [csv_file_name] | ALL (Limit scope to specific subscriptions. Accepts a .csv file with the list of subscriptions.
# If ALL is specified, all subscriptions will be scanned)
# -ResourceGroup [resource_goup] (Limit scope to a specific resoure group)
# -MachineName [machine_name] (Limit scope to a specific machine)
#
#
param (
[Parameter (Mandatory=$false)]
[Parameter (Mandatory=$true)]
[string] $SubId,
[Parameter (Mandatory= $false)]
[string] $ResourceGroup,
[Parameter (Mandatory= $false)]
[string] $MachineName,
[Parameter (Mandatory= $false)]
[boolean] $All=$false
)
[Parameter (Mandatory= $true)]
[string] $MachineName
)
function CheckModule ($m) {
@@ -60,6 +57,7 @@ Update-AzConfig -DisplayBreakingChangeWarning $false
# Load required modules
$requiredModules = @(
"AzureAD",
"Az.Accounts",
"Az.ConnectedMachine",
"Az.ResourceGraph"
@@ -70,13 +68,13 @@ $requiredModules | Foreach-Object {CheckModule $_}
if ($SubId -like "*.csv") {
$subscriptions = Import-Csv $SubId
}elseif($SubId -ne $null){
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription
}elseif($All){
$subscriptions = Get-AzSubscription
}else {
Write-Host ([Environment]::NewLine + "-- Parameter missing --")
exit
}elseif($SubId -like "ALL"){
$subscriptions = Get-AzSubscription -TenantID (Get-AzureADTenantDetail).ObjectId
}elseif($SubId -ne ""){
$subscriptions = [PSCustomObject]@{SubscriptionId = $SubId} | Get-AzSubscription -TenantID (Get-AzureADTenantDetail).ObjectId
}else{
Write-Host ([Environment]::NewLine + "-- Subscription parameter is missing --")
exit
}
Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --")
@@ -94,33 +92,61 @@ foreach ($sub in $subscriptions){
{continue}
}
$query = "
$query1 = "
resources
| where subscriptionId =~ '$($sub.Id)'
| where type =~ 'microsoft.hybridcompute/machines/extensions'
| extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type), provisioningState = tostring(properties.provisioningState)
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
| where extensionPublisher =~ 'Microsoft.AzureData'
| where provisioningState =~ 'Succeeded'
| parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' *
"
$query2 = "
resources
| where subscriptionId =~ '$($sub.Id)'
| where type =~ 'Microsoft.AzureArcData/SqlServerInstances'
| where properties.status =~ 'Connected'
"
if ($ResourceGroup) {
$query1 += "| where resourceGroup =~ '$($ResourceGroup)'"
$query2 += "| where resourceGroup =~ '$($ResourceGroup)'"
}
if ($MachineName) {
$query1 += "| where machineName =~ '$($MachineName)'"
$query2 += "| where name =~ '$($MachineName)'"
}
$query1 += "
| project machineName, extensionName = name, resourceGroup, location, subscriptionId, extensionPublisher, extensionType, properties
"
if ($MachineName) {$query += "| where machineName =~ '$($MachineName)'"}
if ($ResourceGroup) {$query += "| where resourceGroup =~ '$($ResourceGroup)'"}
$resources = Search-AzGraph -Query "$($query) | where subscriptionId =~ '$($sub.Id)'"
#
# Delete Azure extension for SQL
$resources = Search-AzGraph -Query "$($query1)"
foreach ($r in $resources) {
$setID = @{
MachineName = $r.MachineName
ResourceGroup = $r.resourceGroup
Name = $r.extensionName
}
Remove-AzConnectedMachineExtension @SetId -NoWait # | Out-Null
Remove-AzConnectedMachineExtension @SetId -NoWait | Out-Null
}
#
# Remove Arc-enable SQL Server resources
$resources = Search-AzGraph -Query "$($query2)"
foreach ($r in $resources) {
Remove-AzResource -ResourceId $r.resourceId -Force | Out-Null
}
}