-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSQL_Server_Build_Check_Script_Creation.ps1
More file actions
161 lines (136 loc) · 4.03 KB
/
SQL_Server_Build_Check_Script_Creation.ps1
File metadata and controls
161 lines (136 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
Param
(
[string]$OutputScriptFilePath = "SQLChecks\SQL Server Build Version is not latest.sql"
)
# Define the URL
$url = "https://learn.microsoft.com/en-us/troubleshoot/sql/releases/download-and-install-latest-updates"
# Fetch the HTML content
$html = Invoke-WebRequest -Uri $url -TimeoutSec 10
# Check for internet connection
if ($html.StatusCode -eq 200) {
# Add a small delay to ensure content is fully loaded
Start-Sleep -Seconds 5
# Parse the HTML content to extract the first row of each table, skipping the first two tables
$tables = $html.ParsedHtml.getElementsByTagName("table")
$builds = @()
# Iterate through tables, starting from the 2nd one (index 1)
for ($i = 1; $i -lt $tables.length; $i++) {
$table = $tables[$i]
$rows = $table.getElementsByTagName("tr")
# Get the first row (after the header row)
if ($rows.length -gt 1) {
$firstRow = $rows[1] # Skip header and get the first data row
# Get the version from the first column of the first data row
$versionCell = $firstRow.getElementsByTagName("td")[0]
$version = $versionCell.innerText.Trim()
# Add the version to the builds array
$builds += [PSCustomObject]@{
Version = $version
}
}
}
# Convert the parsed data to a DataTable
$dataTable = New-Object System.Data.DataTable
$dataTable.Columns.Add("Version", [string]) | Out-Null
# Populate the DataTable
$builds | ForEach-Object {
$row = $dataTable.NewRow()
$row["Version"] = $_.Version
$dataTable.Rows.Add($row)
}
# Generate SQL script values
$VALUES = ""
foreach ($row in $dataTable.Rows) {
$Version = $($row["Version"])
$VALUES += "('$Version'),"
}
# Remove the trailing comma
$VALUES = $VALUES.TrimEnd(',')
# SQL script header and footer
$SQLBuildScript_Header = "
/*
DESCRIPTION:
Indicate that SQL Server build version is not latest (aka Service Pack / Cummulative Updates / Hotfix / GDR Security Fix).
*/
DECLARE
@CurrentBuildVersion AS NVARCHAR(128) = CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)),
@LatestBuildVersion AS NVARCHAR(128)
SET @AdditionalInfo = NULL;
SELECT
@LatestBuildVersion =
CASE
WHEN (LEN([Values]) - LEN(REPLACE([Values],'.',''))) < 3 THEN CONCAT([Values], '.0')
ELSE [Values]
END
FROM
(
VALUES "
$SQLBuildScript_Footer = "
) AS x([Values])
WHERE
SUBSTRING([Values], 0, CHARINDEX('.', [Values], 0)) = SUBSTRING(@CurrentBuildVersion, 0, CHARINDEX('.', @CurrentBuildVersion, 0));
IF @CurrentBuildVersion < @LatestBuildVersion
BEGIN
SET @AdditionalInfo =
(
SELECT
@LatestBuildVersion AS Latest,
@CurrentBuildVersion AS [Current]
FOR XML
PATH (N'') ,
ROOT (N'BuildVersion')
);
END
INSERT INTO
#Checks
(
CheckId ,
Title ,
RequiresAttention ,
WorstCaseImpact ,
CurrentStateImpact ,
RecommendationEffort ,
RecommendationRisk ,
AdditionalInfo,
[Responsible DBA Team]
)
SELECT
CheckId = @CheckId ,
Title = N'{CheckTitle}' ,
RequiresAttention =
CASE
WHEN @AdditionalInfo IS NULL
THEN 0
ELSE
1
END ,
WorstCaseImpact = 2 , -- Medium
CurrentStateImpact =
CASE
WHEN @AdditionalInfo IS NULL
THEN 0 -- None
ELSE
2 -- Medium
END ,
RecommendationEffort =
CASE
WHEN @AdditionalInfo IS NULL
THEN 0 -- None
ELSE
2 -- Medium
END ,
RecommendationRisk =
CASE
WHEN @AdditionalInfo IS NULL
THEN 0 -- None
ELSE
2 -- Medium
END ,
AdditionalInfo = @AdditionalInfo,
[Responsible DBA Team] = N'Production/Development';
"
# Output the full SQL script to the file
$SQLBuildScript_Header + $VALUES + $SQLBuildScript_Footer | Out-File $OutputScriptFilePath -Force
} else {
Write-Host "Internet connection is not available. Output file will not be changed."
}