From 35f54e88a5040480bb4b46920f5afee8c1a68bd0 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Thu, 6 Apr 2023 20:06:07 -0700 Subject: [PATCH 1/2] Added uninstall extension script --- ...install-azure-extension-for-sql-server.ps1 | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 diff --git a/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 b/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 new file mode 100644 index 00000000..74f0f494 --- /dev/null +++ b/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 @@ -0,0 +1,126 @@ +# +# This script provides a scaleable solution to uninstall Azure extension for SQL Server on a specific Arc-enabled server, +# all Arc-enabled servers in a specific resource group, specific subscription, a list of subscriptions or the entire account. +# +# You can specfy a single subscription to scan, or provide subscriptions as a .CSV file with the list of IDs. +# If not specified, all subscriptions your role has access to are scanned. +# +# 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 cointributor access to). +# +# + +param ( + [Parameter (Mandatory=$false)] + [string] $SubId, + [Parameter (Mandatory= $false)] + [string] $ResourceGroup, + [Parameter (Mandatory= $false)] + [string] $MachineName, + [Parameter (Mandatory= $false)] + [boolean] $All=$false +) + +function CheckModule ($m) { + + # This function ensures that the specified module is imported into the session + # If module is already imported - do nothing + + if (!(Get-Module | Where-Object {$_.Name -eq $m})) { + # If module is not imported, but available on disk then import + if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) { + Import-Module $m + } + else { + + # If module is not imported, not available on disk, but is in online gallery then install and import + if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) { + Install-Module -Name $m -Force -Verbose -Scope CurrentUser + Import-Module $m + } + else { + + # If module is not imported, not available and not in online gallery then abort + write-host "Module $m not imported, not available and not in online gallery, exiting." + EXIT 1 + } + } + } +} + +# +# Suppress warnings +# +Update-AzConfig -DisplayBreakingChangeWarning $false + +# Load required modules +$requiredModules = @( + "Az.Accounts", + "Az.ConnectedMachine", + "Az.ResourceGraph" +) +$requiredModules | Foreach-Object {CheckModule $_} + +# Subscriptions to scan + +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 +} + +Write-Host ([Environment]::NewLine + "-- Scanning subscriptions --") + +# Scan arc-enabled servers in each subscription + +foreach ($sub in $subscriptions){ + + if ($sub.State -ne "Enabled") {continue} + + try { + Set-AzContext -SubscriptionId $sub.Id + }catch { + write-host "[Environment]::NewLine + Invalid subscription: $($sub.Id)" + {continue} + } + + $query = " + resources + | where type =~ 'microsoft.hybridcompute/machines/extensions' + | extend extensionPublisher = tostring(properties.publisher), extensionType = tostring(properties.type), provisioningState = tostring(properties.provisioningState) + | where extensionPublisher =~ 'Microsoft.AzureData' + | where provisioningState =~ 'Succeeded' + | parse id with * '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' * + | 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)'" + foreach ($r in $resources) { + + + + $setID = @{ + MachineName = $r.MachineName + ResourceGroup = $r.resourceGroup + Name = $r.extensionName + } + + Remove-AzConnectedMachineExtension @SetId -NoWait # | Out-Null + + } +} + + \ No newline at end of file From 90f0f6659453612504160c97452d0433a6953e69 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Thu, 6 Apr 2023 20:25:57 -0700 Subject: [PATCH 2/2] add readme --- .../README.md | 117 ++++++++++++++++++ ...install-azure-extension-for-sql-server.ps1 | 2 +- 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/README.md diff --git a/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/README.md b/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/README.md new file mode 100644 index 00000000..8f0a7c20 --- /dev/null +++ b/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/README.md @@ -0,0 +1,117 @@ +--- +services: Azure Arc-enabled SQL Server +platforms: Azure +author: anosov1960 +ms.author: sashan +ms.date: 4/6/2023 +--- + + +# Overview + +This script provides a scalable solution to uninstall Azure extension for SQL Server on a specific Arc-enabled server, +all Arc-enabled servers in a specific resource group, specific subscription, a list of subscriptions or the entire account. +You can specify a single subscription to scan, or provide a list of subscriptions as a .CSV file. + +# Prerequisites + +- 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. + +# Launching the script + +The script accepts the following command line parameters: + +| **Parameter**                                         | **Value**                                                                       | **Description** | +|:--|:--|:--| +|-SubId|subscription_id *or* a file_name|Optional: subscription id or a .csv file with the list of subscriptions1. | +|-ResourceGroup |resource_group_name|Optional: Limit the scope to a specific resource group| +|-MachineName |machine_name|Optional: Limit the scope to a specific machine| +|-All|\$True or \$False (default)|Optional. Uninstall Azure extension on all Arc-enabled servers in all subscriptions you have contributor access to.| + +1You can create a .csv file using the following command and then edit to remove the subscriptions you don't want to scan. +```PowerShell +Get-AzSubscription | Export-Csv .\mysubscriptions.csv -NoTypeInformation +``` + +## Example 1 + +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 +``` + +## Example 2 + +The following command will scan the subscription `` and uninstall Azure extension for SQL Server on all Arc-enabled servers where it is installed. + +```PowerShell +.\uninstall-azure-extension-for-sql-server.ps1 -SubId +``` + +## Example 3 + +The following command will scan resource group in the subscription `` and and uninstall Azure extension for SQL Server on all Arc-enabled servers where it is installed. + +```PowerShell +.\uninstall-azure-extension-for-sql-server.ps1 -SubId -ResourceGroup +``` + +# Running the script using Cloud Shell + +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. + +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: + + ```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. + + ```console + .\uninstall-azure-extension-for-sql-server.ps1 -All $True + ``` + +> [!NOTE] +> - To paste the commands into the shell, use `Ctrl-Shift-V` on Windows or `Cmd-v` on MacOS. +> - The script will be uploaded directly to the home folder associated with your Cloud Shell session. + +# 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: + + ```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 + ``` + +1. Make sure the NuGet package provider is installed: + + ```console + Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser + Install-packageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Scope CurrentUser -Force + ``` + +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): + + ```console + 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). + + ```console + Connect-AzAccount + ``` + +1. Run the script using the desired scope. + + ```console + .\uninstall-azure-extension-for-sql-server.ps1 -All $True + ``` diff --git a/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 b/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 index 74f0f494..3ffa5856 100644 --- a/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/uninstall-azure-extension-for-sql-server/uninstall-azure-extension-for-sql-server.ps1 @@ -11,7 +11,7 @@ # 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 cointributor access to). +# -All (Uninstall Azure extension on all Arc-enabled servers in all subscriptions you have contributor access to). # #