-
Notifications
You must be signed in to change notification settings - Fork 5
/
SQLTranscriptase.psm1
106 lines (83 loc) · 2.55 KB
/
SQLTranscriptase.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
<#
.SYNOPSIS
Provides Common functions for the action scripts
.DESCRIPTION
Provides Common functions for the action scripts
.EXAMPLE
.Inputs
.Outputs
.NOTES
1) Windows/SQL auth server connections
.LINK
#>
function ConnectWinAuth
{
[CmdletBinding()]
Param([String]$SQLExec,
[String]$SQLInstance,
[String]$Database)
Process
{
# Open connection and Execute sql against server using Windows Auth
$DataSet = New-Object System.Data.DataSet
$SQLConnectionString = "Data Source=$SQLInstance;Initial Catalog=$Database;Integrated Security=SSPI;"
$Connection = New-Object System.Data.SqlClient.SqlConnection
$Connection.ConnectionString = $SQLConnectionString
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SQLExec
$SqlCmd.Connection = $Connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$SqlCmd.CommandTimeout=0
# Insert results into Dataset table
$SqlAdapter.Fill($DataSet) |out-null
if ($DataSet.Tables.Count -ne 0)
{
$sqlresults = $DataSet.Tables[0]
}
else
{
$sqlresults =$null
}
# Close connection to sql server
$Connection.Close()
Write-Output $sqlresults
}
}
function ConnectSQLAuth
{
[CmdletBinding()]
Param([String]$SQLExec,
[String]$SQLInstance,
[String]$Database,
[String]$User,
[String]$Password)
Process
{
# Open connection and Execute sql against server using Windows Auth
$DataSet = New-Object System.Data.DataSet
$SQLConnectionString = "Data Source=$SQLInstance;Initial Catalog=$Database;User ID=$User;Password=$Password"
$Connection = New-Object System.Data.SqlClient.SqlConnection
$Connection.ConnectionString = $SQLConnectionString
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SQLExec
$SqlCmd.Connection = $Connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
# Insert results into Dataset table
$SqlAdapter.Fill($DataSet) |out-null
if ($DataSet.Tables.Count -ne 0)
{
$sqlresults = $DataSet.Tables[0]
}
else
{
$sqlresults =$null
}
# Close connection to sql server
$Connection.Close()
Write-Output $sqlresults
}
}
export-modulemember -function ConnectWinAuth
export-modulemember -function ConnectSQLAuth