-
Notifications
You must be signed in to change notification settings - Fork 11
Thanks! #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sid351
wants to merge
2
commits into
IrisClasson:master
Choose a base branch
from
sid351:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Thanks! #1
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # PowerShell_General | ||
|
|
||
| The sql.ps1 file has been reformatted in to separate functions to make reusing the code easier for different queries. | ||
|
|
||
| The functions will need to be loaded before they can be used. This can be achieved in 2 different ways: | ||
|
|
||
| 2. Adding these functions in to a module. | ||
|
|
||
| Modules have a different file extension (psm1) and need to be located in a specific folder structure under the $env:PsModulePath environment variable. | ||
|
|
||
| However once there they are automatically loaded when required in PowerShell V3+ and can be loaded and unloaded manually (using the Import-Module and Remove-Module cmdlets respectively). | ||
|
|
||
| [More information on modules can be found here.](https://technet.microsoft.com/en-us/library/dd878324(v=vs.85).aspx) | ||
|
|
||
| **Modules are the recommended approach for making easily resusable functions in PowerShell.** | ||
|
|
||
| 1. Dot-sourcing the sql.ps1 file. | ||
|
|
||
| Assuming the sql.ps1 file is in the current working directory, this is done by running ". .\sql.ps1" (without quotes). | ||
|
|
||
| It's important to note the space between the first dot (.) and the path to sql.ps1 | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,100 @@ | ||
| $dataSource = ".\SQLEXPRESS" | ||
| $user = "user" | ||
| $pwd = "1234" | ||
| $database = "Test" | ||
| $connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;" | ||
| Function Connect-SQLServer | ||
| { | ||
| [cmdletbinding()] | ||
| Param( | ||
| $dataSource = ".\SQLEXPRESS", | ||
| $database = "Test", | ||
| [Parameter(ParameterSetName="UserAutentication")]$user = "user", | ||
| [Parameter(ParameterSetName="UserAutentication")]$pwd = "1234", | ||
| [Parameter(ParameterSetName="IntegratedSecurtity")][switch]$IntegratedSecurtity | ||
| ) | ||
|
|
||
| $query = "SELECT * FROM Person" | ||
| 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. | ||
|
|
||
| $connection = New-Object System.Data.SqlClient.SqlConnection | ||
| $connection.ConnectionString = $connectionString | ||
| #$connection.ConnectionString = "Server=$dataSource;Database=$database;Integrated Security=True;" | ||
| $connection.Open() | ||
| $command = $connection.CreateCommand() | ||
| $command.CommandText = $query | ||
| Stores the SQL session in the $SqlSession variable for use later. | ||
|
|
||
| $result = $command.ExecuteReader() | ||
| .EXAMPLE $SqlSession = Connect-SQLServer -dataSource "localhost" -database "SlickTicket" -IntegratedSecurtity | ||
| Connects to the SlickTicket DB on Localhost using Integrated Security | ||
|
|
||
| $table = new-object �System.Data.DataTable� | ||
| $table.Load($result) | ||
| Stores the SQL session in the $SqlSession variable for use later. | ||
| #> | ||
|
|
||
| $format = @{Expression={$_.Id};Label="User Id";width=10},@{Expression={$_.Name};Label="Identified Swede"; width=30} | ||
|
|
||
| $table | Where-Object {$_.Surname -like "*sson" -and $_.Born -lt 1990} | format-table $format | ||
| function Get-SQLTableQuery | ||
| { | ||
| [cmdletbinding()] | ||
| Param( | ||
| $SQLSession, | ||
| $Query = "SELECT * FROM Person" | ||
| ) | ||
|
|
||
| $table | Where-Object {$_.Surname -like "*sson" -and $_.Born -lt 1990} | format-table $format | Out-File C:\Users\Iris\Documents\swedes.txt | ||
| $command = $SQLSession.CreateCommand() | ||
| $command.CommandText = $Query | ||
|
|
||
| $connection.Close() | ||
| $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 | ||
| #> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting the table view