forked from IrisClasson/PowerShell_General
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.ps1
More file actions
100 lines (74 loc) · 2.96 KB
/
sql.ps1
File metadata and controls
100 lines (74 loc) · 2.96 KB
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
Function Connect-SQLServer
{
[cmdletbinding()]
Param(
$dataSource = ".\SQLEXPRESS",
$database = "Test",
[Parameter(ParameterSetName="UserAutentication")]$user = "user",
[Parameter(ParameterSetName="UserAutentication")]$pwd = "1234",
[Parameter(ParameterSetName="IntegratedSecurtity")][switch]$IntegratedSecurtity
)
If($IntegratedSecurtity)
{
$connectionString = "Server=$dataSource;Database=$database;Integrated Security=True;"
}
Else
{
$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
}
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
Write-Output $connection
}
<#
.EXAMPLE $SqlSession = Connect-SQLServer
Uses all the default values to replicate the original script.
Stores the SQL session in the $SqlSession variable for use later.
.EXAMPLE $SqlSession = Connect-SQLServer -dataSource "localhost" -database "SlickTicket" -IntegratedSecurtity
Connects to the SlickTicket DB on Localhost using Integrated Security
Stores the SQL session in the $SqlSession variable for use later.
#>
function Get-SQLTableQuery
{
[cmdletbinding()]
Param(
$SQLSession,
$Query = "SELECT * FROM Person"
)
$command = $SQLSession.CreateCommand()
$command.CommandText = $Query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($result)
Write-Output $table
}
<#
.EXAMPLE
$results = Get-SQLTableQuery -SQLSession $SqlSession
$format = @{Expression={$_.Id};Label="User Id";width=10},@{Expression={$_.Name};Label="Identified Swede"; width=30}
$results | Where-Object {$_.Surname -like "*sson" -and $_.Born -lt 1990} | format-table $format
$results | Where-Object {$_.Surname -like "*sson" -and $_.Born -lt 1990} | format-table $format | Out-File C:\Users\Iris\Documents\swedes.txt
Uses all the default values to replicate the original script.
1) Collects the output of the SQL query in to the $results variable
2) Sets the formatting parameters for the host stream
3) Returns the formatted table (a collection of string, not an object) to the host stream
4) Pipes the fortmatted table (a collection of string, not an object) to C:\Users\Iris\Documents\swedes.txt
.EXAMPLE
$results = Get-SQLTableQuery -SQLSession $SqlSession -Query "Select * from faq"
$results | Export-CSV -Path "Exported-FAQ-Table.csv" -NoTypeInformation
1) Collects the output of the "Select * from faq" SQL query in to the $results variable
2) Exports the SQL Table object as a CSV to the "Exported-FAQ-Table.csv" file in the current working directory
#>
Function Disconnect-SQLServer
{
[cmdletbinding()]
Param(
$SQLSession
)
$SQLSession.Close()
}
<#
.EXAMPLE Disconnect-SQLServer -SQLSession $SqlSession
Cleanly closes the SQL connection
#>