Skip to content

Commit 76db1cf

Browse files
authored
Merge pull request #1206 from MikeRayMSFT/set-monitoring
Add script to set monitoring
2 parents ed92fed + cabcc3f commit 76db1cf

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<#
2+
.SYNOPSIS
3+
.
4+
.DESCRIPTION
5+
.
6+
.Parameter MachineName
7+
The name of the Arc Sql Server-enabled machine on which to update extension settings.
8+
.Parameter resourceGroup
9+
The Azure resource group hosting the machine.
10+
.PARAMETER Subscription
11+
The Azure subscription for the machine.
12+
.PARAMETER FeatureFlagsToEnable
13+
Optional. Array of the names of feature flags to be enabled in the extension's settings.
14+
Unknown feature flags will generate a warning, and may generate a prompt to continue
15+
(unless the Force flag is provided).
16+
.PARAMETER FeatureFlagsToDisable
17+
Optional. Array of the names of feature flags to be disabled in the extension's settings.
18+
Unknown feature flags will generate a warning, and may generate a prompt to continue
19+
(unless the Force flag is provided).
20+
.PARAMETER Force
21+
Optional. Provide the Force flag to update the settings even if unrecognized feature flags
22+
are provided.
23+
.PARAMETER DryRun
24+
Optional. Provide the DryRun flag to retrieve current settings and modify them, but do not
25+
send the modified settings to the extension. DryRun can be used to verify changes before
26+
executing them.
27+
.EXAMPLE
28+
To enable the SqlManagement and AG discovery features in your subscription
29+
in resource group contoso-rg for machine contoso-sql-host, the command would be:
30+
SetArcSqlServerFeatureFlags.ps1 `
31+
-Subscription "<Your-subscription-ID>" `
32+
-ResourceGroup "contoso-rg" `
33+
-MachineName "contoso-sql-host" `
34+
-FeatureFlagsToEnable ("AvailabilityGroupDiscovery", "SqlManagement")
35+
36+
.NOTES
37+
Author: Sarah Feitler
38+
Date: September 2023
39+
#>
40+
param(
41+
[Parameter (Mandatory = $true)]
42+
[string] $Subscription,
43+
[Parameter (Mandatory = $true)]
44+
[string] $ResourceGroup,
45+
[Parameter (Mandatory = $true)]
46+
[string] $MachineName,
47+
[Parameter (Mandatory = $false)]
48+
[string[]] $FeatureFlagsToEnable,
49+
[Parameter (Mandatory = $false)]
50+
[string[]] $FeatureFlagsToDisable,
51+
[Parameter (Mandatory = $false)]
52+
[switch] $Force,
53+
[Parameter (Mandatory = $false)]
54+
[switch] $DryRun
55+
)
56+
57+
# This list comes from the Azure Sql Server Extension project, and may not be up-to-date.
58+
$knownFeatureFlags = "AvailabilityGroupDiscovery", "SqlManagement", "SqlFailoverClusterInstanceDiscovery"
59+
60+
$mysteryFlagsList = New-Object System.Collections.ArrayList
61+
62+
if ($FeatureFlagsToEnable -ne $null)
63+
{
64+
$FeatureFlagsToEnable = $FeatureFlagsToEnable | Get-Unique
65+
# using output as a throwaway to control console output
66+
$output = $FeatureFlagsToEnable | Where-Object {
67+
if ( $_ -notin $knownFeatureFlags )
68+
{
69+
$mysteryFlagsList.Add("$_ ")
70+
$true
71+
}
72+
else
73+
{
74+
$false
75+
}
76+
}
77+
}
78+
if ($FeatureFlagsToDisable -ne $null)
79+
{
80+
$FeatureFlagsToDisable = $FeatureFlagsToDisable | Get-Unique
81+
# using output as a throwaway to control console output
82+
$output = $FeatureFlagsToDisable | Where-Object {
83+
if ( $_ -notin $knownFeatureFlags )
84+
{
85+
$mysteryFlagsList.Add("$_ ")
86+
$true
87+
}
88+
else
89+
{
90+
$false
91+
}
92+
}
93+
}
94+
95+
if ($mysteryFlagsList.Count -gt 0)
96+
{
97+
Write-Host ("Warning, the following flags were not found in known list of feature flags: $mysteryFlagsList")
98+
if ($Force -eq $false) {
99+
$confirmation = Read-Host "Do you want to continue executing the script? (Y/N)"
100+
# Check the user's response
101+
if ($confirmation -eq "Y" -or $confirmation -eq "y") {
102+
# User wants to continue, script execution continues
103+
Write-Host "Continuing..."
104+
} else {
105+
# User chose not to continue, so exit the script
106+
Write-Host "Exiting"
107+
exit 1
108+
}
109+
}
110+
}
111+
112+
$connectedMachineExtension = az extension list | where-object {$_ -like "*connectedmachine*"}
113+
if($connectedMachineExtension.Count -eq 0){
114+
Write-Output "connectedmachine extension is not installed. Please run the following command to install it before running this script."
115+
Write-Output "az extension add -n connectedmachine"
116+
Exit 1
117+
}
118+
119+
# Retrieve current settings and convert to a powershell object
120+
$jsonData = az connectedmachine extension show --name "WindowsAgent.SqlServer" --machine-name $machineName --resource-group $ResourceGroup --subscription $Subscription | Join-String
121+
122+
if (! $?)
123+
{
124+
Write-Host
125+
Write-Host "Error on extension show. Exiting."
126+
exit 1
127+
}
128+
129+
try {
130+
$object = $jsonData | ConvertFrom-Json
131+
}
132+
catch {
133+
Write-Output "Failed to retrieve valid json. This may be caused because the connectedmachine extension was not properly installed prior to running the script. Please run the script again."
134+
Exit 1
135+
}
136+
137+
if($null -eq $object.properties.settings.FeatureFlags)
138+
{
139+
# create new featureFlags array if it does not exist.
140+
$object.properties.settings | Add-Member -Name "FeatureFlags" -Value @() -MemberType NoteProperty
141+
}
142+
143+
# Check if FeatureFlagsToEnable or FeatureFlagsToDisable arrays are specified
144+
if ($FeatureFlagsToEnable -ne $null -or $FeatureFlagsToDisable -ne $null) {
145+
$foundFeatureFlags = $object.properties.settings.FeatureFlags.Name
146+
147+
if ($FeatureFlagsToEnable -ne $null) {
148+
foreach ($flagToEnable in $FeatureFlagsToEnable) {
149+
if ($flagToEnable -in $foundFeatureFlags) {
150+
# Update the Enable property for the flag
151+
$object.properties.settings.FeatureFlags | Where-Object { $_.Name -eq $flagToEnable } | ForEach-Object { $_.Enable = $true }
152+
} else {
153+
# Create a new entry for the flag
154+
$newFlag = @{
155+
Enable = $true
156+
Name = $flagToEnable
157+
}
158+
$object.properties.settings.FeatureFlags += $newFlag
159+
}
160+
}
161+
}
162+
163+
if ($FeatureFlagsToDisable -ne $null) {
164+
foreach ($flagToDisable in $FeatureFlagsToDisable) {
165+
if ($flagToDisable -in $foundFeatureFlags) {
166+
# Update the Enable property for the flag
167+
$object.properties.settings.FeatureFlags | Where-Object { $_.Name -eq $flagToDisable } | ForEach-Object { $_.Enable = $false }
168+
} else {
169+
# Create a new entry for the flag
170+
$newFlag = @{
171+
Enable = $false
172+
Name = $flagToDisable
173+
}
174+
$object.properties.settings.FeatureFlags += $newFlag
175+
}
176+
}
177+
}
178+
}else{
179+
Write-Output "No feature flags were enabled or disabled."
180+
Exit
181+
}
182+
183+
# Convert the settings section of the modified object back to JSON
184+
$currentSettings = $object.properties.settings
185+
$updatedJsonData = $currentSettings | ConvertTo-Json -Depth 10
186+
187+
Write-Host "Updated settings: $updatedJsonData"
188+
Set-Content -Path .\patchedJson.json -Value $updatedJsonData
189+
190+
if ( $DryRun -eq $False )
191+
{
192+
write-host "This may take up to 5 minutes to complete. Please wait."
193+
az connectedmachine extension update `
194+
--name "WindowsAgent.SqlServer" `
195+
--type "WindowsAgent.SqlServer" `
196+
--publisher "Microsoft.AzureData" `
197+
--settings patchedJson.json `
198+
--machine-name $machineName `
199+
--resource-group $resourceGroup `
200+
--subscription $subscription
201+
Write-Host "Updated settings in extension"
202+
}
203+
else
204+
{
205+
Write-Host "DryRun run done. Updated settings can be viewed in the file $pwd\patchedjson.json"
206+
}

0 commit comments

Comments
 (0)