Created
March 10, 2014 15:34
-
-
Save 40a/9467238 to your computer and use it in GitHub Desktop.
Invoke-Sqlcmd3
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
| Function Invoke-Sqlcmd3 { | |
| <# | |
| .SYNOPSIS | |
| Runs a T-SQL script on one or more Instances of SQL | |
| .DESCRIPTION | |
| Runs a T-SQL script. Invoke-Sqlcmd2 only returns message output, such as the output of PRINT statements when -verbose parameter is specified. | |
| Paramaterized queries are supported. | |
| Help details below borrowed from Invoke-Sqlcmd | |
| .PARAMETER ComputerName | |
| One or more ServerInstances to query. For default instances, only specify the computer name: "MyComputer". For named instances, use the format "ComputerName\InstanceName". | |
| .PARAMETER Database | |
| A character string specifying the name of a database. Invoke-Sqlcmd2 connects to this database in the instance that is specified in -ServerInstance. | |
| .PARAMETER Query | |
| Specifies one or more queries to be run. The queries can be Transact-SQL (? or XQuery statements, or sqlcmd commands. Multiple queries separated by a semicolon can be specified. Do not specify the sqlcmd GO separator. Escape any double quotation marks included in the string ?). Consider using bracketed identifiers such as [MyTable] instead of quoted identifiers such as "MyTable". | |
| .PARAMETER InputFile | |
| Specifies a file to be used as the query input to Invoke-Sqlcmd2. The file can contain Transact-SQL statements, (? XQuery statements, and sqlcmd commands and scripting variables ?). Specify the full path to the file. | |
| .PARAMETER Credential | |
| Specifies A PSCredential for SQL Server Authentication connection to an instance of the Database Engine. If -Credential is not specified, Invoke-Sqlcmd attempts a Windows Authentication connection using the Windows account running the PowerShell session. | |
| SECURITY NOTE: If you use the -Debug switch, the connectionstring including plain text password will be sent to the debug stream. | |
| .PARAMETER Username | |
| Specifies the login ID for making a SQL Server Authentication connection to an instance of the Database Engine. The password must be specified using -Password. If -Username and -Password or -credential are not specified, Invoke-Sqlcmd attempts a Windows Authentication connection using the Windows account running the PowerShell session. | |
| When possible, use Windows Authentication. | |
| .PARAMETER Password | |
| Specifies the password for the SQL Server Authentication login ID that was specified in -Username. Passwords are case-sensitive. When possible, use Windows Authentication. Do not use a blank password, when possible use a strong password. For more information, see "Strong Password" in SQL Server Books Online. | |
| SECURITY NOTE: If you type -Password followed by your password, the password is visible to anyone who can see your monitor. If you code -Password followed by your password in a .ps1 script, anyone reading the script file will see your password. Assign the appropriate NTFS permissions to the file to prevent other users from being able to read the file. | |
| .PARAMETER QueryTimeout | |
| Specifies the number of seconds before the queries time out. | |
| .PARAMETER ConnectionTimeout | |
| Specifies the number of seconds when Invoke-Sqlcmd2 times out if it cannot successfully connect to an instance of the Database Engine. The timeout value must be an integer between 0 and 65534. If 0 is specified, connection attempts do not time out. | |
| .PARAMETER As | |
| Specifies output type - DataSet, DataTable, array of DataRow, PSObject or Single Value | |
| PSObject output introduces overhead but adds flexibility for working with results: http://powershell.org/wp/forums/topic/dealing-with-dbnull/ | |
| .PARAMETER AppendServerInstance | |
| If specified, append the server instance to PSObject and DataRow output | |
| .INPUTS | |
| None | |
| You cannot pipe objects to Invoke-Sqlcmd2 | |
| .OUTPUTS | |
| System.Data.DataTable | |
| .EXAMPLE | |
| Invoke-Sqlcmd2 -ServerInstance "MyComputer\MyInstance" -Query "SELECT login_time AS 'StartTime' FROM sysprocesses WHERE spid = 1" | |
| This example connects to a named instance of the Database Engine on a computer and runs a basic T-SQL query. | |
| StartTime | |
| ----------- | |
| 2010-08-12 21:21:03.593 | |
| .EXAMPLE | |
| Invoke-Sqlcmd2 -ServerInstance "MyComputer\MyInstance" -InputFile "C:\MyFolder\tsqlscript.sql" | Out-File -filePath "C:\MyFolder\tsqlscript.rpt" | |
| This example reads a file containing T-SQL statements, runs the file, and writes the output to another file. | |
| .EXAMPLE | |
| Invoke-Sqlcmd2 -ServerInstance "MyComputer\MyInstance" -Query "PRINT 'hello world'" -Verbose | |
| This example uses the PowerShell -Verbose parameter to return the message output of the PRINT command. | |
| VERBOSE: hello world | |
| .EXAMPLE | |
| Invoke-Sqlcmd2 -ServerInstance MyServer\MyInstance -Query "SELECT ServerName, VCNumCPU FROM tblServerInfo" -as PSObject | ?{$_.VCNumCPU -gt 8} | |
| Invoke-Sqlcmd2 -ServerInstance MyServer\MyInstance -Query "SELECT ServerName, VCNumCPU FROM tblServerInfo" -as PSObject | ?{$_.VCNumCPU} | |
| This example uses the PSObject output type to allow more flexibility when working with results. Using a datarow would result in errors for the first example, and would include rows where VCNumCPU has DBNull value. | |
| .EXAMPLE | |
| 'Instance1', 'Server1/Instance1', 'Server2' | Invoke-Sqlcmd2 -query "Sp_databases" -as psobject -AppendServerInstance | |
| This example lists databases for each instance. It includes a column for the ServerInstance in question. | |
| DATABASE_NAME DATABASE_SIZE REMARKS ServerInstance | |
| ------------- ------------- ------- -------------- | |
| REDACTED 88320 Instance1 | |
| master 17920 Instance1 | |
| msdb 161472 Instance1 | |
| REDACTED 158720 Instance1 | |
| tempdb 8704 Instance1 | |
| REDACTED 92416 Server1/Instance1 | |
| master 7744 Server1/Instance1 | |
| msdb 618112 Server1/Instance1 | |
| REDACTED 10004608 Server1/Instance1 | |
| REDACTED 153600 Server1/Instance1 | |
| tempdb 563200 Server1/Instance1 | |
| master 5120 Server2 | |
| msdb 215552 Server2 | |
| OperationsManager 20480000 Server2 | |
| tempdb 8704 Server2 | |
| .NOTES | |
| Version History | |
| poshcode.org - http://poshcode.org/4967 | |
| v1.0 - Chad Miller - Initial release | |
| v1.1 - Chad Miller - Fixed Issue with connection closing | |
| v1.2 - Chad Miller - Added inputfile, SQL auth support, connectiontimeout and output message handling. Updated help documentation | |
| v1.3 - Chad Miller - Added As parameter to control DataSet, DataTable or array of DataRow Output type | |
| v1.4 - Justin Dearing <zippy1981 _at_ gmail.com> - Added the ability to pass parameters to the query. | |
| v1.4.1 - Paul Bryson <atamido _at_ gmail.com> - Added fix to check for null values in parameterized queries and replace with [DBNull] | |
| v1.5 - Joel Bennett - add SingleValue output option | |
| v1.5.1 - RamblingCookieMonster - Added ParameterSets, set Query and InputFile to mandatory | |
| v1.5.2 - RamblingCookieMonster - Added DBNullToNull switch and code from Dave Wyatt. Added parameters to comment based help (need someone with SQL expertise to verify these) | |
| github.com - https://github.com/RamblingCookieMonster/PowerShell | |
| v1.5.3 - RamblingCookieMonster - Replaced DBNullToNull param with PSObject Output option. Added credential support. Added pipeline support for ServerInstance. Added to GitHub | |
| RamblingCookieMonster - Added AppendServerInstance switch. | |
| .LINK | |
| https://github.com/RamblingCookieMonster/PowerShell | |
| #> | |
| [CmdletBinding(DefaultParametersetName="Server")] | |
| Param( | |
| [Parameter( Mandatory=$true, | |
| ValueFromPipelineByPropertyName =$true, | |
| HelpMessage="SQL Server required...", | |
| ParameterSetName="Server")] | |
| [Alias("Instance","Servers","SQLInstance")] | |
| [ValidateNotNull()] | |
| [string[]] | |
| $ComputerName, | |
| [Parameter( Mandatory=$false, | |
| ValueFromPipelineByPropertyName =$true)] | |
| [string] | |
| $Database, | |
| [Parameter( Mandatory=$false, | |
| ValueFromPipelineByPropertyName =$true)] | |
| [Parameter( Mandatory=$false)] | |
| [string] | |
| $Query, | |
| [Parameter( Mandatory=$false)] | |
| [Parameter( ParameterSetName="UserPass")] | |
| [string] | |
| $Username, | |
| [Parameter( Mandatory=$false)] | |
| [Parameter( ParameterSetName="UserPass")] | |
| [string] | |
| $Password, | |
| [Parameter( Mandatory=$false)] | |
| [System.Management.Automation.PSCredential] | |
| [System.Management.Automation.Credential()] | |
| $Credential = [System.Management.Automation.PSCredential]::Empty, | |
| [Parameter( Mandatory=$false)] | |
| [Int32] | |
| $QueryTimeout=600, | |
| [Parameter( Mandatory=$false)] | |
| [Int32] | |
| $ConnectionTimeout=15, | |
| [Parameter( Mandatory=$false)] | |
| [ValidateScript({test-path $_})] | |
| [Parameter( ParameterSetName="Query")] | |
| [string] | |
| $InputFile, | |
| [Parameter( Mandatory=$false)] | |
| [ValidateSet("DataSet", "DataTable", "DataRow","SingleValue","PSObject")] | |
| [string] | |
| $As="DataRow", | |
| [Parameter( Mandatory=$false)] | |
| [System.Collections.IDictionary] | |
| $SqlParameters | |
| [Parameter( Mandatory=$false)] | |
| [switch] | |
| $AppendServerInstance = $FALSE | |
| ) | |
| BEGIN { | |
| Try { | |
| Try { add-type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -EA Stop } | |
| Catch { add-type -AssemblyName "Microsoft.SqlServer.SMOExtended" -EA Stop } | |
| } | |
| Catch { Throw "SMOExtended not available" } | |
| If ( (Get-PSSnapin -Name SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue) -eq $NULL ) { Add-PSSnapin SqlServerCmdletSnapin100 } | |
| If ( (Get-PSSnapin -Name SqlServerProviderSnapin100 -ErrorAction SilentlyContinue) -eq $NULL ) { Add-PSSnapin SqlServerProviderSnapin100 } | |
| If ($InputFile) { | |
| $filePath = $(resolve-path $InputFile).path | |
| $Query = [System.IO.File]::ReadAllText("$filePath") | |
| $Commands = [regex]::Split($Query, '\bGO') | |
| } | |
| If ($Query) { | |
| $Commands = [regex]::Split($Query, '\bGO') | |
| } | |
| Write-Verbose "Running Invoke-Sqlcmd2 with ParameterSet $($PSCmdlet.ParameterSetName). Performing query '$Query'" | |
| If ($Username -AND !$Password) { | |
| $Credential = Get-Credential -Credential $Username | |
| } | |
| $credSplat = @{} | |
| If ($Credential -ne [System.Management.Automation.PSCredential]::Empty) { | |
| $credSplat['Credential'] = $Credential | |
| } | |
| If($As -eq "PSObject") { | |
| #This code scrubs DBNulls. Props to Dave Wyatt | |
| $cSharp = @' | |
| using System; | |
| using System.Data; | |
| using System.Management.Automation; | |
| public class DBNullScrubber | |
| { | |
| public static PSObject DataRowToPSObject(DataRow row) | |
| { | |
| PSObject psObject = new PSObject(); | |
| if (row != null && (row.RowState & DataRowState.Detached) != DataRowState.Detached) | |
| { | |
| foreach (DataColumn column in row.Table.Columns) | |
| { | |
| Object value = null; | |
| if (!row.IsNull(column)) | |
| { | |
| value = row[column]; | |
| } | |
| psObject.Properties.Add(new PSNoteProperty(column.ColumnName, value)); | |
| } | |
| } | |
| return psObject; | |
| } | |
| } | |
| '@ | |
| Try | |
| { | |
| Add-Type -TypeDefinition $cSharp -ReferencedAssemblies 'System.Data','System.Xml' -ErrorAction stop | |
| } | |
| Catch | |
| { | |
| If(-not $_.ToString() -like "*The type name 'DBNullScrubber' already exists*") | |
| { | |
| Write-Warning "Could not load DBNullScrubber. Defaulting to DataRow output: $_" | |
| $As = "Datarow" | |
| } | |
| } | |
| } | |
| $Conn=New-Object System.Data.SqlClient.SQLConnection | |
| } | |
| PROCESS { | |
| ForEach ($Computer in $ComputerName) { | |
| If (Test-Connection -Quiet -Count 2 -ComputerName $Computer -Split '/') { | |
| If ($Credential) { | |
| $ConnectionString = "Server=$Computer;Database=$Database;User ID=$($Credential.UserName);Password=$($Credential.GetNetworkCredential().Password);Trusted_Connection=False;Connect Timeout=$ConnectionTimeout" | |
| } | |
| ElseIf ($Username -AND $Password) { | |
| $ConnectionString = "Server=$Computer;Database=$Database;User ID=$Username;Password=$Password;Trusted_Connection=False;Connect Timeout=$ConnectionTimeout " | |
| } | |
| Else { | |
| $ConnectionString = "Server=$Computer;Database=$Database;Integrated Security=True;Connect Timeout=$ConnectionTimeout" | |
| } | |
| $conn.ConnectionString=$ConnectionString | |
| #Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller | |
| If ($PSBoundParameters.Verbose) { | |
| $Conn.FireInfoMessageEventOnUserErrors=$true | |
| $Handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {Write-Verbose "$($_)"} | |
| $Conn.add_InfoMessage($Handler) | |
| } | |
| Try { $conn.Open() } | |
| Catch { | |
| Write-Error $_ | |
| continue | |
| } | |
| $DS=New-Object System.Data.DataSet | |
| ForEach ($Command in $Commands) { | |
| Try { | |
| $Cmd = New-Object System.Data.Sqlclient.Sqlcommand($Command, $Conn); | |
| $Cmd.CommandTimeout=$QueryTimeout | |
| If ($SqlParameters -ne $null) { | |
| $SqlParameters.GetEnumerator() | | |
| ForEach-Object { | |
| If ($_.Value -ne $null) | |
| { $cmd.Parameters.AddWithValue($_.Key, $_.Value) } | |
| Else | |
| { $cmd.Parameters.AddWithValue($_.Key, [DBNull]::Value) } | |
| } > $null | |
| } | |
| $DA=New-Object System.Data.SqlClient.SqlDataAdapter($cmd) | |
| [void]$DA.Fill($DS) | |
| Switch ($As) { | |
| 'DataSet' { Write-Output ($ds) } | |
| 'DataTable' { Write-Output ($ds.Tables) } | |
| 'DataRow' { Write-Output ($ds.Tables[0]) } | |
| 'SingleValue' { Write-Output ($ds.Tables[0] | Select-Object -Expand $ds.Tables[0].Columns[0].ColumnName ) } | |
| 'PSObject' { #Scrub DBNulls - Provides convenient results you can use comparisons with | |
| #Introduces overhead (e.g. ~2000 rows w/ ~80 columns went from .15 Seconds to .65 Seconds - depending on your data could be much more!) | |
| ForEach ($row in $ds.Tables[0].Rows) { | |
| [DBNullScrubber]::DataRowToPSObject($row) | |
| } | |
| } | |
| } | |
| } | |
| Catch { | |
| [system.exception] | |
| $_.Exception | |
| } | |
| } | |
| $conn.Close() | |
| } | |
| Else { | |
| Write-Error "Test-Connection failed to $($Computer -Split '/')" | |
| } | |
| } | |
| } | |
| END { $conn.Close() } | |
| } #Invoke-Sqlcmd3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment