-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataMigration.ps1
279 lines (236 loc) · 8.32 KB
/
DataMigration.ps1
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
Param
(
[Parameter(Mandatory=$true)]
[string] $ProjectName = 'LucientMigration',
[Parameter(Mandatory=$true)]
[string] $SQLInstance,
[Parameter(Mandatory=$true)]
[string] $ResultOutputPath,
[ValidateSet("SqlServer2016","SqlServerWindows2017","SqlServerWindows2019","AzureSqlDatabase","ManagedSqlServer")]
[string] $Target,
[ValidateSet("JSON")]
[string]$output,
[string] $ErrorReport
)
<#
.SYNOPSIS
A powershell script to run Microsoft Data Migration tool in command mode (DmaCmd)
.REQUIREMENTS
MS Data Migration Assistant installed
- https://www.microsoft.com/en-us/download/confirmation.aspx?id=53595
- https://blogs.msdn.microsoft.com/datamigration/2016/10/25/data-migration-assistant-configuration-settings/
Powershell module SQLPS
.DESCRIPTION
Script take 5 mandatory parameters,
- Projectname
- SQLInstance
- ResultOutputPath Full path
- Target Validate against SQL Server 2016/2017/2019 on Windows or in Azure SQL Azure Database or Managed Sql Server
- Output Save result as JSON
.NOTES
Auther: Lucient Denmark
Torben Schou ([email protected])
.SAMPLE
.\DataMigration.ps1 -ProjectName "LucientMigration" -SQLInstance ".\myInstance" -ResultOutputPath "C:\result\" -Target "Choose from List" -Ouput "JSON"
#>
Clear-Host
$Global:file = $ErrorReport
#region Verify that Microsoft Data Migration Tool are installed
Function IsInstalled ()
{
try
{
$FullName = (Get-ChildItem -ErrorAction SilentlyContinue -path 'C:\Program Files' -Filter "dmacmd.exe" -Recurse ).FullName
}
catch
{
$message = "`r`n$_"
Add-Content $Global:file $message
}
if (!($FullName))
{
Write-Progress "Data Migration Tool not found on host $Env:Computername!" -ForegroundColor DarkRed -BackgroundColor Yellow
$message = "`r`nData Migration Tool not found on host $Env:Computername!"
Add-Content $Global:file $message
break;
}
$WorkDir = Get-ChildItem -path 'C:\Program Files' -Recurse -Include dmacmd.exe | Select-Object -ExpandProperty Directory
return $WorkDir
}
#endregion Verify that Microsoft Data Migration Tool are installed
#region Log
Function WriteToLog($TxtBase)
{
$ts1 = get-date -Format "yyyy-MM-dd HH:mm:ss"
$TxtB = "[$ts1] `n"
$TxtB += "$TxtBase `n"
Add-Content -Path $Global:file -Value $TxtB
}
#endregion Log
#region Get all userdatabases on SQL Instance
Function GetDatabasesOnInstance([string] $SQLInstance, [string] $Target)
{
$message = "Start processing SQL Instance $SQLInstance - target $Target "
WriteToLog $message
if (Get-module -ListAvailable -name SQLSERVER)
{
try
{
$flag = $true
Write-Host "Retrieving databases from SQL Server Instance $SQLInstance - using SQL Module ...."
WriteToLog "Retrieving databases from SQL Server Instance $SQLInstance - using SQL Module ...."
$mydatabases = Get-SqlInstance -ServerInstance $SQLInstance -ErrorAction Stop | Get-SqlDatabase | Where-Object { $_.ID -gt 4 } | Select-Object name
}
catch
{
$flag = $false
$ex = $_.Exception
$message = "ERROR - Failed on retieving database information from $SQLInstance `n"
$message += $ex.message
WriteToLog $message
}
}
elseif (Get-module -ListAvailable -name SQLPS)
{
try
{
$flag= $true
Write-Host "Retrieving databases from SQL Server Instance $SQLInstance - using SQLPS ...."
WriteToLog "INFO - Retrieving databases from SQL Server Instance $SQLInstance - using SQLPS ...."
$srv = New-Object 'Microsoft.SqlServer.Management.SMO.Server' $SQLInstance
$mydatabases = ($srv.Databases | Where-Object ID -GT 4).name
}
catch
{
$flag = $false
$ex = $_.Exception
$message = $SQLInstance + " `n"
$message = $ex.message
WriteToLog $message
}
}
if ($mydatabases.Count -eq 0)
{
Write-Host "WARNING - No databases could be found in SQL Instance $SQLInstance"
$message = "WARNING - No databases could be found in SQL Instance $SQLInstance `n"
WriteToLog $message
} else {
foreach ($x in $mydatabases)
{
$list += "`r`n" + $x.Name.ToString()
}
$list += "`r`n"
$message = "These databases `n"
$message += "$list `n"
$message += "will be assessed `n"
WriteToLog $message
}
return $mydatabases
}
#endregion Get all userdatabases on SQL Instance
#region Build database Array
Function DatabasesArray([string]$SQLInstance, [string] $Target)
{
$GetDatabasesOnInstance = GetDatabasesOnInstance $SQLInstance $Target
foreach ($i IN $GetDatabasesOnInstance)
{
$DatabaseName = $i.Name.ToString()
$Connection += '"Server='+$SQLInstance+';Initial Catalog='+$DatabaseName+';Integrated Security=true;"'
$Connection = $Connection + ' '
}
#$Connection = '"' + $Connection
return $Connection
}
#endregion Build database Array
Function GetDbTargetVersion ([string] $mySQLInstance, [string] $myTarget)
{
$control = $true
#Validating of Instance version against requested target
switch ($myTarget)
{
"SqlServer2016"
{
$ver = 13
}
"SqlServerWindows2017"
{
$ver = 14
}
"SqlServerWindows2019"
{
$ver = 15
}
}
try {
$myVer = (Get-SqlInstance -ServerInstance $mySQLInstance -ConnectionTimeout 3).VersionMajor
}
catch {
$message = "ERROR - Can't connect to SQL Server instance $SQLInstance`n"
WriteToLog $message
Break;
}
if ($Ver -le $myver)
{
$message = "WARNING - Will not assess migration to a lower version ($myTarget) of existing SQL Instance $SQLInstance - build version $myVer`n"
WriteToLog $message
$control = $false
} else {
$message = "INFO - $SQLInstance Will be assess for migration to $myTarget`n"
WriteToLog $message
}
return $control
}
$WorkDir = IsInstalled
$mycontrol = $true
if ($Target -in "SqlServer2016","SqlServerWindows2017","SqlServerWindows2019")
{
$mycontrol = GetDbTargetVersion $SQLInstance $Target
}
if ($mycontrol)
{
$DatabasesArray = DatabasesArray $SQLInstance $Target
if ($DatabasesArray.Count -gt 0)
{
$SQLInstance = $SQLInstance.Replace("\", "_")
switch ($output)
{
"JSON"
{
$ArgList = @(
'/AssessmentName="'+ $ProjectName +'" ',
'/AssessmentDatabases='+ $DatabasesArray ,
'/AssessmentEvaluateCompatibilityIssues',
'/AssessmentTargetPlatform='+ $Target ,
'/AssessmentOverwriteResult ',
'/AssessmentResultJson="' + $ResultOutputPath + $SQLInstance + "_" + $Target +'.json" '
)
}
"CSV"
{
$ArgList = @(
'/AssessmentName="'+ $ProjectName +'" ',
'/AssessmentDatabases='+ $DatabasesArray ,
'/AssessmentEvaluateCompatibilityIssues',
'/AssessmentTargetPlatform='+ $Target ,
'/AssessmentOverwriteResult ',
'/AssessmentResultCsv="' + $ResultOutputPath + $SQLInstance + "_" + $Target + '.csv" '
)
}
"All"
{
$ArgList = @(
'/AssessmentName="'+ $ProjectName +'" ',
'/AssessmentDatabases='+ $DatabasesArray ,
'/AssessmentEvaluateCompatibilityIssues',
'/AssessmentTargetPlatform='+ $Target ,
'/AssessmentOverwriteResult ',
'/AssessmentResultCsv="' + $ResultOutputPath + $SQLInstance + "_" + $Target + '.csv" ',
'/AssessmentResultJson="' + $ResultOutputPath + $SQLInstance + "_" + $Target +'.json" '
)
}
}
Start-Process "dmacmd.exe" -ArgumentList $ArgList -WorkingDirectory $WorkDir
}
$message = "SQL Instance $SQLInstance has been proccessed for target $Target"
WriteToLog $message
}