-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
Invoke-Sqlcmd2.ps1
563 lines (477 loc) · 23.4 KB
/
Invoke-Sqlcmd2.ps1
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
function Invoke-Sqlcmd2
{
<#
.SYNOPSIS
Runs a T-SQL script.
.DESCRIPTION
Runs a T-SQL script. Invoke-Sqlcmd2 runs the whole scipt and only captures the first selected result set, such as the output of PRINT statements when -verbose parameter is specified.
Paramaterized queries are supported.
Help details below borrowed from Invoke-Sqlcmd
.PARAMETER ServerInstance
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.
If a SQLConnection is provided, we explicitly switch to this database
.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 Encrypt
If specified, will request that the connection to the SQL is done over SSL. This requires that the SQL Server has been set up to accept SSL requests. For information regarding setting up SSL on SQL Server, visit this link: https://technet.microsoft.com/en-us/library/ms189067(v=sql.105).aspx
.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 SqlParameters
Hashtable of parameters for parameterized SQL queries. http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/
Example:
-Query "SELECT ServerName FROM tblServerInfo WHERE ServerName LIKE @ServerName"
-SqlParameters @{"ServerName = "c-is-hyperv-1"}
.PARAMETER AppendServerInstance
If specified, append the server instance to PSObject and DataRow output
.PARAMETER SQLConnection
If specified, use an existing SQLConnection.
We attempt to open this connection if it is closed
.INPUTS
None
You cannot pipe objects to Invoke-Sqlcmd2
.OUTPUTS
As PSObject: System.Management.Automation.PSCustomObject
As DataRow: System.Data.DataRow
As DataTable: System.Data.DataTable
As DataSet: System.Data.DataTableCollectionSystem.Data.DataSet
As SingleValue: Dependent on data type in first column.
.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.
If we used DataRow rather than PSObject, we would see the following behavior:
Each row where VCNumCPU does not exist would produce an error in the first example
Results would include rows where VCNumCPU has DBNull value in the second example
.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 618112 Server1/Instance1
tempdb 563200 Server1/Instance1
...
OperationsManager 20480000 Server2
.EXAMPLE
#Construct a query using SQL parameters
$Query = "SELECT ServerName, VCServerClass, VCServerContact FROM tblServerInfo WHERE VCServerContact LIKE @VCServerContact AND VCServerClass LIKE @VCServerClass"
#Run the query, specifying values for SQL parameters
Invoke-Sqlcmd2 -ServerInstance SomeServer\NamedInstance -Database ServerDB -query $query -SqlParameters @{ VCServerContact="%cookiemonster%"; VCServerClass="Prod" }
ServerName VCServerClass VCServerContact
---------- ------------- ---------------
SomeServer1 Prod cookiemonster, blah
SomeServer2 Prod cookiemonster
SomeServer3 Prod blah, cookiemonster
.EXAMPLE
Invoke-Sqlcmd2 -SQLConnection $Conn -Query "SELECT login_time AS 'StartTime' FROM sysprocesses WHERE spid = 1"
This example uses an existing SQLConnection and runs a basic T-SQL query against it
StartTime
-----------
2010-08-12 21:21:03.593
.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
- Added AppendServerInstance switch.
- Updated OutputType attribute, comment based help, parameter attributes (thanks supersobbie), removed username/password params
- Added help for sqlparameter parameter.
- Added ErrorAction SilentlyContinue handling to Fill method
v1.6.0 - Added SQLConnection parameter and handling. Is there a more efficient way to handle the parameter sets?
- Fixed SQLConnection handling so that it is not closed (we now only close connections we create)
v1.6.1 - Shiyang Qiu - Fixed the verbose option and SQL error handling conflict
v1.6.2 - Shiyang Qiu - Fixed the .DESCRIPTION.
- Fixed the non SQL error handling and added Finally Block to close connection.
.LINK
https://github.com/RamblingCookieMonster/PowerShell
.LINK
New-SQLConnection
.LINK
Invoke-SQLBulkCopy
.LINK
Out-DataTable
.FUNCTIONALITY
SQL
#>
[CmdletBinding( DefaultParameterSetName='Ins-Que' )]
[OutputType([System.Management.Automation.PSCustomObject],[System.Data.DataRow],[System.Data.DataTable],[System.Data.DataTableCollection],[System.Data.DataSet])]
param(
[Parameter( ParameterSetName='Ins-Que',
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
HelpMessage='SQL Server Instance required...' )]
[Parameter( ParameterSetName='Ins-Fil',
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
HelpMessage='SQL Server Instance required...' )]
[Alias( 'Instance', 'Instances', 'ComputerName', 'Server', 'Servers' )]
[ValidateNotNullOrEmpty()]
[string[]]
$ServerInstance,
[Parameter( Position=1,
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false)]
[string]
$Database,
[Parameter( ParameterSetName='Ins-Que',
Position=2,
Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[Parameter( ParameterSetName='Con-Que',
Position=2,
Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[string]
$Query,
[Parameter( ParameterSetName='Ins-Fil',
Position=2,
Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[Parameter( ParameterSetName='Con-Fil',
Position=2,
Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[ValidateScript({ Test-Path $_ })]
[string]
$InputFile,
[Parameter( ParameterSetName='Ins-Que',
Position=3,
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false)]
[Parameter( ParameterSetName='Ins-Fil',
Position=3,
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false)]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter( ParameterSetName='Ins-Que',
Position=4,
Mandatory=$false,
ValueFromRemainingArguments=$false)]
[Parameter( ParameterSetName='Ins-Fil',
Position=4,
Mandatory=$false,
ValueFromRemainingArguments=$false)]
[switch]
$Encrypt,
[Parameter( Position=5,
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[Int32]
$QueryTimeout=600,
[Parameter( ParameterSetName='Ins-Fil',
Position=6,
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[Parameter( ParameterSetName='Ins-Que',
Position=6,
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[Int32]
$ConnectionTimeout=15,
[Parameter( Position=7,
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[ValidateSet("DataSet", "DataTable", "DataRow","PSObject","SingleValue")]
[string]
$As="DataRow",
[Parameter( Position=8,
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false )]
[System.Collections.IDictionary]
$SqlParameters,
[Parameter( Position=9,
Mandatory=$false )]
[switch]
$AppendServerInstance,
[Parameter( ParameterSetName = 'Con-Que',
Position=10,
Mandatory=$false,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false,
ValueFromRemainingArguments=$false )]
[Parameter( ParameterSetName = 'Con-Fil',
Position=10,
Mandatory=$false,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false,
ValueFromRemainingArguments=$false )]
[Alias( 'Connection', 'Conn' )]
[ValidateNotNullOrEmpty()]
[System.Data.SqlClient.SQLConnection]
$SQLConnection
)
Begin
{
if ($InputFile)
{
$filePath = $(Resolve-Path $InputFile).path
$Query = [System.IO.File]::ReadAllText("$filePath")
}
Write-Verbose "Running Invoke-Sqlcmd2 with ParameterSet '$($PSCmdlet.ParameterSetName)'. Performing query '$Query'"
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"
}
}
}
#Handle existing connections
if($PSBoundParameters.ContainsKey('SQLConnection'))
{
if($SQLConnection.State -notlike "Open")
{
Try
{
Write-Verbose "Opening connection from '$($SQLConnection.State)' state"
$SQLConnection.Open()
}
Catch
{
Throw $_
}
}
if($Database -and $SQLConnection.Database -notlike $Database)
{
Try
{
Write-Verbose "Changing SQLConnection database from '$($SQLConnection.Database)' to $Database"
$SQLConnection.ChangeDatabase($Database)
}
Catch
{
Throw "Could not change Connection database '$($SQLConnection.Database)' to $Database`: $_"
}
}
if($SQLConnection.state -like "Open")
{
$ServerInstance = @($SQLConnection.DataSource)
}
else
{
Throw "SQLConnection is not open"
}
}
}
Process
{
foreach($SQLInstance in $ServerInstance)
{
Write-Verbose "Querying ServerInstance '$SQLInstance'"
if($PSBoundParameters.Keys -contains "SQLConnection")
{
$Conn = $SQLConnection
}
else
{
if ($Credential)
{
$ConnectionString = "Server={0};Database={1};User ID={2};Password=`"{3}`";Trusted_Connection=False;Connect Timeout={4};Encrypt={5}" -f $SQLInstance,$Database,$Credential.UserName,$Credential.GetNetworkCredential().Password,$ConnectionTimeout,$Encrypt
}
else
{
$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2};Encrypt={3}" -f $SQLInstance,$Database,$ConnectionTimeout,$Encrypt
}
$conn = New-Object System.Data.SqlClient.SQLConnection
$conn.ConnectionString = $ConnectionString
Write-Debug "ConnectionString $ConnectionString"
Try
{
$conn.Open()
}
Catch
{
Write-Error $_
continue
}
}
#Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller
if ($PSBoundParameters.Verbose)
{
$conn.FireInfoMessageEventOnUserErrors=$false # Shiyang, $true will change the SQL exception to information
$handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] { Write-Verbose "$($_)" }
$conn.add_InfoMessage($handler)
}
$cmd = New-Object system.Data.SqlClient.SqlCommand($Query,$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
}
$ds = New-Object system.Data.DataSet
$da = New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
Try
{
[void]$da.fill($ds)
}
Catch [System.Data.SqlClient.SqlException] # For SQL exception
{
$Err = $_
Write-Verbose "Capture SQL Error"
if ($PSBoundParameters.Verbose) {Write-Verbose "SQL Error: $Err"} #Shiyang, add the verbose output of exception
switch ($ErrorActionPreference.tostring())
{
{'SilentlyContinue','Ignore' -contains $_} {}
'Stop' { Throw $Err }
'Continue' { Throw $Err}
Default { Throw $Err}
}
}
Catch # For other exception
{
Write-Verbose "Capture Other Error"
$Err = $_
if ($PSBoundParameters.Verbose) {Write-Verbose "Other Error: $Err"}
switch ($ErrorActionPreference.tostring())
{
{'SilentlyContinue','Ignore' -contains $_} {}
'Stop' { Throw $Err}
'Continue' { Throw $Err}
Default { Throw $Err}
}
}
Finally
{
#Close the connection
if(-not $PSBoundParameters.ContainsKey('SQLConnection'))
{
$conn.Close()
}
}
if($AppendServerInstance)
{
#Basics from Chad Miller
$Column = New-Object Data.DataColumn
$Column.ColumnName = "ServerInstance"
$ds.Tables[0].Columns.Add($Column)
Foreach($row in $ds.Tables[0])
{
$row.ServerInstance = $SQLInstance
}
}
switch ($As)
{
'DataSet'
{
$ds
}
'DataTable'
{
$ds.Tables
}
'DataRow'
{
$ds.Tables[0]
}
'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)
}
}
'SingleValue'
{
$ds.Tables[0] | Select-Object -ExpandProperty $ds.Tables[0].Columns[0].ColumnName
}
}
}
}
} #Invoke-Sqlcmd2