Skip to content

Commit 68d479f

Browse files
author
Charmaine Chan
committed
Add host level script
1 parent 4b7225b commit 68d479f

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
$subscriptionId = ""
2+
$resourceGroupName = ""
3+
$whatIf = $null
4+
5+
if ([string]::IsNullOrEmpty($subscriptionId)) {
6+
$subscriptionId = Read-Host -Prompt "Please enter the subscription ID"
7+
}
8+
9+
if ([string]::IsNullOrEmpty($resourceGroupName)) {
10+
$resourceGroupName = Read-Host -Prompt "Please enter the resource group name"
11+
}
12+
13+
if ([string]::IsNullOrEmpty($whatIf)) {
14+
$dryRun = Read-Host -Prompt "Would you like to run this as a dry run? (yes/no)"
15+
if ($dryRun -eq "yes") {
16+
$whatIf = $true
17+
} else {
18+
$whatIf = $false
19+
}
20+
}
21+
22+
if ([string]::IsNullOrEmpty($resourceGroupName) -or [string]::IsNullOrEmpty($subscriptionId)) {
23+
Write-Host "Either the resource group or subscription ID is not specified. Please provide both to proceed."
24+
exit
25+
} else {
26+
Write-Host "Performing actions on ResourceGroup: $($resourceGroupName) in Subscription:$($subscriptionId)"
27+
}
28+
29+
$query = @"
30+
Resources
31+
| where type == 'microsoft.azurearcdata/sqlserverinstances'
32+
| where resourceGroup == '$resourceGroupName'
33+
| where subscriptionId == '$subscriptionId'
34+
| project name, resourceGroup
35+
"@
36+
37+
$minSupportedApiVersion = '2024-07-10'
38+
39+
$resources = Search-AzGraph -Query $query
40+
41+
if ([string]::IsNullOrEmpty($resources)) {
42+
Write-Host "No resources were found in this resource group."
43+
exit
44+
}
45+
46+
$resources = $resources | ForEach-Object {
47+
[pscustomobject]@{
48+
ResourceGroup = $_.resourceGroup
49+
SqlArcResource = $_.name
50+
}
51+
}
52+
Write-Host $resources
53+
$arcMachineResourceIds = @()
54+
foreach ($resource in $resources) {
55+
Write-Host "ResourceGroup: $($resource.ResourceGroup), Sql Arc resource: $($resource.SqlArcResource)"
56+
57+
$hybridComputeResourceId = Get-AzResource -ResourceName $resource.SqlArcResource -ResourceGroupName $resource.ResourceGroup -ResourceType "Microsoft.AzureArcData/sqlServerInstances" | Select-Object -ExpandProperty Properties | Select-Object -ExpandProperty containerResourceId
58+
$arcMachineResourceIds += $hybridComputeResourceId
59+
}
60+
61+
$arcMachineUniqueResourceIds = $arcMachineResourceIds | Get-Unique
62+
Write-Output "Arc Machine Resource Ids:"
63+
Write-Output $arcMachineUniqueResourceIds
64+
65+
foreach ($arcMachineUniqueResourceId in $arcMachineUniqueResourceIds) {
66+
Write-Host "----- Attempting to remove settings from machine: $($arcMachineUniqueResourceId) -----"
67+
$computeMachineResource = Get-AzResource -ResourceId "$arcMachineUniqueResourceId"
68+
$extensionResource = Get-AzResource -ResourceId "$arcMachineUniqueResourceId/extensions/WindowsAgent.SqlServer" -ApiVersion $minSupportedApiVersion
69+
$currentSettings = $extensionResource.properties.settings
70+
71+
$parsedData = @{
72+
"ExtensionAgentStatus" = $null
73+
"TimestampUTC" = $null
74+
}
75+
76+
if ($extensionResource.properties.instanceView.status -match "SQL Server Extension Agent: (\w+);") {
77+
$parsedData["ExtensionAgentStatus"] = $matches[1]
78+
}
79+
80+
if ($extensionResource.properties.instanceView.status -match "timestampUTC : ([\d\/:., ]+);") {
81+
$parsedData["TimestampUTC"] = [datetime]::ParseExact($matches[1], "yyyy/MM/dd, HH:mm:ss.fff", $null)
82+
}
83+
84+
# Check if the Extension Agent is healthy and the timestamp is within the last 24 hours
85+
$extensionAgentHealthy = $parsedData["ExtensionAgentStatus"] -eq "Healthy"
86+
$timestampWithin24Hours = ($parsedData["TimestampUTC"] -gt (Get-Date).AddHours(-24))
87+
88+
if ($computeMachineResource.properties.status -ne "Connected") {
89+
Write-Host "This machine has status: $($computeMachineResource.properties.status). We will skip removing the configurations on this machine."
90+
continue
91+
} elseif (-not ($extensionAgentHealthy -and $timestampWithin24Hours)) {
92+
Write-Host "The extension agent status is: $($parsedData["ExtensionAgentStatus"]) and was last updated: $($($parsedData["TimestampUTC"]))."
93+
Write-Host "The extension status must be healthy and updated within 24hrs for us to proceed. We will skip removing the configurations on this machine."
94+
continue
95+
} else {
96+
Write-Host "This machine has status: $($computeMachineResource.properties.status). We will proceed to remove the configurations."
97+
}
98+
99+
# Disable ESU
100+
if ($currentSettings.PSobject.Properties.Name -contains "EnableExtendedSecurityUpdates") {
101+
$currentSettings.EnableExtendedSecurityUpdates = $false
102+
}
103+
# Disable Microsoft Updates
104+
if ($currentSettings.PSobject.Properties.Name -contains "MicrosoftUpdateConfiguration") {
105+
$currentSettings.MicrosoftUpdateConfiguration.EnableMicrosoftUpdate = $false
106+
}
107+
# Disable BPA
108+
if ($currentSettings.PSobject.Properties.Name -contains "AssessmentSettings") {
109+
$currentSettings.AssessmentSettings.Enable = $false
110+
}
111+
112+
$newProperties = $extensionResource.properties
113+
$newProperties.settings = $currentSettings
114+
115+
$newProperties | ConvertTo-Json | Out-File "settingsInFile.json"
116+
117+
try {
118+
if ($whatIf) {
119+
$extensionResource | Set-AzResource -Properties $newProperties -UsePatchSemantics -Pre -ErrorAction Stop -WhatIf -AsJob
120+
} else {
121+
$extensionResource | Set-AzResource -Properties $newProperties -UsePatchSemantics -Pre -ErrorAction Stop -Force -AsJob
122+
}
123+
Write-Host "Command executed."
124+
} catch {
125+
Write-Host "Command failed with the following error:"
126+
Write-Host $_.Exception.Message
127+
}
128+
}

0 commit comments

Comments
 (0)