Invoke-MySqlQuery.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
<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 0a3629db-9d0f-4b9c-a75f-d708778ee444
 
.AUTHOR Warren F.
 
.TAGS
    MySql, Query, Sql
 
.LICENSEURI
    https://github.com/RamblingCookieMonster/PowerShell/blob/master/LICENSE
 
.PROJECTURI
    https://github.com/RamblingCookieMonster/PowerShell
 
.DESCRIPTION
    Runs a SQL script against a MySQL instance. Requires the ADO.NET driver for MySQL - http://dev.mysql.com/downloads/connector/net/
#>


function Invoke-MySQLQuery { 
    <#
    .SYNOPSIS
        Runs a SQL script against a MySQL instance
 
    .DESCRIPTION
        Runs a SQL script against a MySQL instance
 
        Paramaterized queries are supported.
 
        Help details below borrowed from Invoke-Sqlcmd
 
        MySQL specific considerations should be taken.
            For example, consider evaluating the risks around differing connections string options - http://dev.mysql.com/doc/connector-net/en/connector-net-connection-options.html
            Contributions to this function would be appreciated : )
         
        IMPORTANT - This is a first draft. As may be apparent, I am not a MySQL guy : )
                    Requires the ADO.NET driver for MySQL - http://dev.mysql.com/downloads/connector/net/
 
    .PARAMETER ComputerName
        One or more servers to query.
 
    .PARAMETER Database
        A character string specifying the name of a database. Invoke-MySQLQuery connects to this database in the instance that is specified in -ServerInstance.
 
    .PARAMETER Port
        TCP Port to connect to MySQL over
 
    .PARAMETER Query
        Specifies a query to be run.
 
    .PARAMETER InputFile
        Specifies a file to be used as the query input to Invoke-MySQLQuery. Specify the full path to the file.
 
    .PARAMETER Credential
        Specifies A PSCredential for authentication connection to an instance of the Database Engine.
        SECURITY NOTE: If you use the -Debug switch, the connectionstring including plain text password will be sent to the debug stream.
        SECURITY NOTE: Read up on connection string options, and evaluate the string provided in this function...
 
    .PARAMETER QueryTimeout
        Specifies the number of seconds before the queries time out.
 
    .PARAMETER ConnectionTimeout
        Specifies the number of seconds when Invoke-MySQLQuery times out if it cannot successfully connect to an instance of the Database Engine.
 
    .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 MySQL server name and port to PSObject or DataRow output
 
    .INPUTS
        ServerInstance
            You can pipe SQL Instance names to Invoke-MySqlQuery. The query will execute against each instance.
 
    .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-MySQLQuery -ComputerName MyServer -Query "SELECT ServerName, VCNumCPU FROM tblServerInfo" -as PSObject | ?{$_.VCNumCPU -gt 8}
        Invoke-MySQLQuery -ComputerName MyServer -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
        'Server1', 'Server2', 'Server3' | Invoke-MySQLQuery -query "SHOW DATABASES" -as psobject -AppendServerInstance
 
        This example lists databases for each instance. It includes a column for the MySQL server and ports in question.
            Database MySQLServer MySQLPort
            -------- ----------- ---------
            information_schema Server1 3306
            information_schema Server2 3306
            information_schema Server3 3306
 
    .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-MySQLQuery -ComputerName 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
 
    .NOTES
        Version History
         
        codeplex.com - http://sqlpsx.codeplex.com/
         
        github.com - https://github.com/RamblingCookieMonster/PowerShell
        v0.1.0 - Merged SQLPSX mySQLLib functions from Mike Shepard into Invoke-SqlCmd2 function
 
    .LINK
        https://github.com/RamblingCookieMonster/PowerShell
 
    .FUNCTIONALITY
        SQL
    #>


    [CmdletBinding( DefaultParameterSetName='Query' )]
    [OutputType([System.Management.Automation.PSCustomObject],[System.Data.DataRow],[System.Data.DataTable],[System.Data.DataTableCollection],[System.Data.DataSet])]
    param(
        [Parameter( Position=0,
                    Mandatory=$true,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ValueFromRemainingArguments=$false,
                    HelpMessage='SQL Server Instance required...' )]
        [Alias( 'Instance', 'Instances', 'ServerInstance', 'Server', 'Servers','cn' )]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $ComputerName,

        [Parameter( Position=1,
                    Mandatory=$false,
                    ValueFromPipelineByPropertyName=$true,
                    ValueFromRemainingArguments=$false )]
        [int]$Port = 3306,

        [Parameter( Position=2,
                    Mandatory=$false,
                    ValueFromPipelineByPropertyName=$true,
                    ValueFromRemainingArguments=$false)]
        [string]
        $Database,
    
        [Parameter( Position=3,
                    Mandatory=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ValueFromRemainingArguments=$false,
                    ParameterSetName='Query' )]
        [string]
        $Query,
        
        [Parameter( Position=3,
                    Mandatory=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ValueFromRemainingArguments=$false,
                    ParameterSetName="File")]
        [ValidateScript({ Test-Path $_ })]
        [string]
        $InputFile,
        
        [Parameter( Position=4,
                    Mandatory=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ValueFromRemainingArguments=$false,
                    ParameterSetName="Query")]
        [Parameter( Position=4,
                    Mandatory=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ValueFromRemainingArguments=$false,
                    ParameterSetName="File")]
        [System.Management.Automation.PSCredential]
        $Credential,

        [Parameter( Position=5,
                    Mandatory=$false,
                    ValueFromPipelineByPropertyName=$true,
                    ValueFromRemainingArguments=$false )]
        [Int32]
        $QueryTimeout=600,
    
        [Parameter( 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
    ) 

    Begin
    {
        
        if( -not ($Library = [System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")) )
        {
            Throw "This function requires the ADO.NET driver for MySQL:`n`thttp://dev.mysql.com/downloads/connector/net/"
        }
        

        if ($InputFile) 
        { 
            $filePath = $(Resolve-Path $InputFile).path 
            $Query =  [System.IO.File]::ReadAllText("$filePath") 
        }

        Write-Verbose "Running Invoke-MySQLQuery 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"
                }
            }
        }
    }
    Process
    {
        foreach($Computer in $ComputerName)
        {
            Write-Verbose "Querying ComputerName '$Computer'"

            $ConnectionString = "Server={0};Port=$Port;Database={1};Uid={2};Pwd={3};allow zero datetime=yes;Connection Timeout={4}" -f $Computer,$Database,$Credential.UserName,$Credential.GetNetworkCredential().Password,$ConnectionTimeout
            
            $conn=new-object MySql.Data.MySqlClient.MySqlConnection
            $conn.ConnectionString = $ConnectionString 
            Write-Debug "ConnectionString $ConnectionString"

            <# TODO Check if this is needed
             
            #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
            }

            $cmd = New-Object MySql.Data.MySqlClient.MySqlCommand($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 MySql.Data.MySqlClient.MySqlDataAdapter($cmd)
    
            Try
            {
                [void]$da.fill($ds)
                $conn.Close()
            }
            Catch
            { 
                $Err = $_
                $conn.Close()

                switch ($ErrorActionPreference.tostring())
                {
                    {'SilentlyContinue','Ignore' -contains $_} {}
                    'Stop' {     Throw $Err }
                    'Continue' { Write-Error $Err}
                    Default {    Write-Error $Err}
                }              
            }

            if($AppendServerInstance)
            {
                #Basics from Chad Miller
                $Column =  New-Object Data.DataColumn
                $Column.ColumnName = "MySQLServer"
                $ds.Tables[0].Columns.Add($Column)
                Foreach($row in $ds.Tables[0])
                {
                    $row.MySQLServer = $Computer
                }
                
                $Column =  New-Object Data.DataColumn
                $Column.ColumnName = "MySQLPort"
                $ds.Tables[0].Columns.Add($Column)
                Foreach($row in $ds.Tables[0])
                {
                    $row.MySQLPort = $Port
                }
            }

            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
                }
            }
        }
    }
}