forked from psake/psake-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.psm1
64 lines (54 loc) · 2.07 KB
/
database.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
function Invoke-SqlCommand {
[cmdletbinding(DefaultParameterSetName='customauth')]
param(
[Parameter(Mandatory=$false)]
[string]
$sqlServer = ".\SQLEXPRESS",
[Parameter(Mandatory=$false)]
[string]
$database = "Northwind",
[Parameter(Mandatory=$true)]
[string]
$sqlCommand,
[Parameter(Mandatory=$false, ParameterSetName='credauth')]
[System.Management.Automation.PsCredential]
$credential,
[Parameter(Mandatory=$false, ParameterSetName='customauth')]
[string]
$authentication ="Integrated Security=SSPI;",
[Parameter(ParameterSetName='devauth')]
[switch]
$developmentAuthentication
)
switch ($PsCmdlet.ParameterSetName) {
'devauth' {
$authentication = 'User ID=sa;Password=pass'
Write-Debug "Using development authentication: $authentication"
}
'credauth' {
$plainCred = $credential.GetNetworkCredential()
$authentication = "uid={0};pwd={1};" -f $plainCred.Username,$plainCred.Password
Write-Debug "Using passed credentials, user: $($plainCred.Username)"
}
'customauth' {
Write-Debug "Using custom authentication: $authentication"
}
default { throw "Parameter set name unknown: $($PsCmdlet.ParameterSetName)" }
}
$connectionString = "Server=$sqlServer;Database=$database;$authentication;"
write-debug "Connection string: $connectionString"
## Connect to the data source and open it
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $connectionString
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $sqlCommand
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$dataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($dataSet)
$SqlConnection.Close()
$SqlCmd.Dispose()
## Return all of the rows from their query
$dataSet.Tables | Select-Object -Expand Rows
}