-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathHelpers.ps1
45 lines (40 loc) · 1.33 KB
/
Helpers.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
# This function always assumes SQL Authentication. Update if needed.
function Invoke-SqlLCommand {
param(
[string] $dataSource,
[string] $sqlCommand,
[string] $sqlUserName,
[string] $sqlPassword
)
$connectionString = "Data Source=$dataSource; User Id=$sqlUserName; Password=$sqlPassword;";
try{
$connection = New-Object System.Data.SqlClient.SQLConnection($connectionString)
$commandObj = New-Object System.Data.SqlClient.SqlCommand($sqlCommand, $connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $commandObj
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
# return the response
$dataSet.Tables
}
catch{
Write-Error "$_" -ErrorAction Continue
}
}
# Function gets all DBs from source database.
function Get-DatabasesToMigrate {
param(
[string] $dataSource,
[string] $sqlUserName,
[string] $sqlPassword,
[string] $sqlQueryToGetDbs
)
try{
$sqlDbs = Invoke-SqlLCommand -dataSource $dataSource -sqlUserName $sqlUserName -sqlPassword $sqlPassword -sqlCommand $sqlQueryToGetDbs
$sqlDbs | ForEach-Object { $_.name };
}
catch{
Write-Error "$_" -ErrorAction Continue
}
}