forked from dsccommunity/xMySql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSFT_xMySqlUtilities.psm1
323 lines (264 loc) · 7.54 KB
/
MSFT_xMySqlUtilities.psm1
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
function Invoke-MySqlCommand
{
<#
.SYNOPSIS
This function runs the given command with the given arguments. This
is done so that the function can be mocked for unit testing.
.EXAMPLE
Invoke-MySqlCommand -CommandPath "C:\somepath.exe" -Arguments "test", "test2"
.PARAMETER CommandPath
This is the path to the command you want to run.
.PARAMETER Arguments
These are the arguments you want to supply to the command.
#>
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $CommandPath,
[parameter(Mandatory = $true)]
[string[]] $Arguments
)
if (Test-Path -Path $CommandPath)
{
& $CommandPath $Arguments
}
else
{
Throw "$CommandPath does not exist"
}
}
function Get-MySqlInstallerConsole
{
<#
.SYNOPSIS
This function returns the path for the MySQL Installer Console.
.EXAMPLE
Get-MySqlInstallerConsole
C:\Program Files (x86)\MySQL\MySQL Installer for Windows\MySQLInstallerConsole.exe
#>
$mySqlInstallerConsole = Join-Path -Path "$(${env:ProgramFiles(x86)})\MySQL\MySQL Installer for Windows" -ChildPath "MySQLInstallerConsole.exe"
# Throw an exception if MySQL Installer for Windows isn't installed
if (-not (Test-Path -Path $mySqlInstallerConsole))
{
Throw 'Please ensure that MySQL Installer for Windows is installed'
}
return $mySqlInstallerConsole
}
function Get-MySqlExe
{
<#
.SYNOPSIS
This function takes in a version number and returns the path of
mysql.exe for that version. It checks to see if the x64
version is installed and then if the x86 version is
installed. If neither is installed it throws an error.
.EXAMPLE
Get-MySqlExe -MySqlVersion "5.6.17"
C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe
.EXAMPLE
Get-MySqlExe -MySqlVersion "5.6.17"
C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin\mysql.exe
.PARAMETER MySqlVersion
The version of MySQL you want the mysql.exe path for.
#>
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $MySqlVersion
)
$shortVersion = Get-ShortVersion -MySqlVersion $MySqlVersion
$mySqlExeX64 = Join-Path -Path "$(${env:ProgramFiles})\MySQL\MySQL Server $shortVersion\bin" -ChildPath "mysql.exe"
$mySqlExeX86 = Join-Path -Path "$(${env:ProgramFiles(x86)})\MySQL\MySQL Server $shortVersion\bin" -ChildPath "mysql.exe"
# Throw an exception if the path doesn't exist
if (Test-Path -Path $mySqlExeX64)
{
return $mySqlExeX64
}
elseif (Test-Path -Path $mySqlExeX86)
{
return $mySqlExeX86
}
else
{
Throw "Please ensure that MySQL Version $shortVersion is installed"
}
}
function Get-MySqlVersionInstalled
{
<#
.SYNOPSIS
This function takes in a version number and determines if that
version of MySQL is installed.
.EXAMPLE
Get-MySqlVersionInstalled -MySqlVersion "5.6.17"
True
.EXAMPLE
Get-MySqlVersionInstalled -MySqlVersion "5.6.17"
False
.PARAMETER MySqlVersion
The MySQL version you want to know about.
#>
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $MySqlVersion
)
$MySqlInstalled = $false
$arguments = "community", "status"
$statusResults = (Invoke-MySqlCommand -CommandPath $(Get-MySqlInstallerConsole) -Arguments $arguments) -split "`r`n"
ForEach ($statusResult in $statusResults)
{
if ($statusResult -eq "MySQL Server $MySqlVersion")
{
$MySqlInstalled = $true
}
}
return $MySqlInstalled
}
function Get-MySqlAllInstalled
{
<#
.SYNOPSIS
This function returns all of the versions of MySQL
that are installed.
.EXAMPLE
Get-MySqlAllInstalled
5.6.17
5.7.8
.EXAMPLE
Get-MySqlAllInstalled
5.6.17
#>
$allVersionsInstalled = @()
$arguments = "community", "status"
$statusResults = (Invoke-MySqlCommand -CommandPath $(Get-MySqlInstallerConsole) -Arguments $arguments) -split "`r`n"
ForEach ($statusResult in $statusResults)
{
if ($statusResult -match "^MySQL Server")
{
# get the version number from the $statusResult
$splitStatus = $statusResult.Split(" ")
$allVersionsInstalled += ,$splitStatus[2]
}
}
return $allVersionsInstalled
}
function Get-ShortVersion
{
<#
.SYNOPSIS
This function takes in a version number and returns the shortened
version of it.
.EXAMPLE
Get-ShortVersion -MySqlVersion "5.6.17"
5.6
.EXAMPLE
Get-ShortVersion -MySqlVersion "5.7.8"
5.7
.PARAMETER MySqlVersion
This is the long version number that you want shortened.
#>
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $MySqlVersion
)
$splitVersion = $MySqlVersion.Split(".")
$shortVersion = [string]::Join(".",$splitVersion,0,2)
return $shortVersion
}
function Read-ErrorFile
{
<#
.SYNOPSIS
This function reads an error file and throws an error if there
is a line that starts off with ERROR.
.EXAMPLE
Read-ErrorFile -ErrorFilePath "C:\errorfile.txt"
.PARAMETER ErrorFilePath
This is the path of the error file you want read.
#>
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $ErrorFilePath
)
if (Test-Path -Path $ErrorFilePath)
{
$content = Get-Content $ErrorFilePath
ForEach ($line in $content)
{
if ($line -match "^ERROR")
{
Remove-Item -Path $ErrorFilePath
Throw $content -join "`n"
}
}
Remove-Item -Path $ErrorFilePath
}
}
function Get-MySqlPort
{
<#
.SYNOPSIS
This function looks at the my.ini file of the version passed in
and returns the port number from the file.
.EXAMPLE
Get-MySqlPort -MySqlVersion "5.6.17"
3306
.PARAMETER MySqlVersion
This is the version you want the port number for.
.PARAMETER MySqlIniPath
The optional path override to the MySQL Ini file containing the port configuration
#>
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $MySqlVersion,
[string] $MySqlIniPath = $null
)
if ([string]::IsNullOrEmpty($MySqlIniPath)){
$MySqlIniPath = Join-Path "$(${env:ProgramData})\MySQL\MySQL Server $(Get-ShortVersion $MySqlVersion)" -ChildPath "my.ini"
}
# Throw an exception if $MySqlIniPath doesn't exist
if (-not (Test-Path -Path $MySqlIniPath))
{
Throw 'The my.ini file does not exist in the standard location'
}
ForEach ($line in (Get-Content $MySqlIniPath))
{
if ($line -match "^port\s*=")
{
$mySqlPort = ($line -split '=')[1].Trim()
}
}
return $mySqlPort
}
function Get-ArchitectureName
{
<#
.SYNOPSIS
This function returns the architecture of the server.
.EXAMPLE
Get-ArchitectureName
x64
.EXAMPLE
Get-ArchitectureName
x86
#>
if($env:PROCESSOR_ARCHITECTURE -eq "AMD64")
{
return "x64"
}
else
{
return "x86"
}
}