Invoke-SqlScripts
Executes an ordered list of SQL scripts. Optionally collects their outputs into a text file.
If a file in the list has a .CSV extension, then it will be converted to a set of INSERT
statements and executed as described in the documentation for cmdlet Import-CsvTable
.
The -SqlFiles
parameter provides an array of regex patterns. Files will be recursively located in and beneath directory -SqlDir
, and executed or imported in the order listed. If a given regex pattern returns more than one result, then files returned for that pattern will be executed or imported in the order returned.
Syntax
Invoke-SqlScripts `
[-DbServer <string>] `
[-DbName <string>] `
[-SqlDir <string>] `
[-SqlFiles <string>] `
[-OutputPath <string>] `
[-OutputTable]
Parameters
Parameter | R | Description |
---|---|---|
DbServer | * | The target SQL Server database server address. Example: -DbServer "MyDbServer" |
DbName | * | The target SQL Server database name. Example: -DbServer "MyDb" |
SqlDir | The directory containing the SQL scripts to be executed. Relative paths will be resolved relative to the current directory. Default: $pwd Example: -SqlDir "./ResultSql" |
|
SqlFiles | A comma-delimited list of RegEx patterns indicating SQL script files to be executed and/or CSV data files to be imported. All files must be nested within the directory indicated by -SqlDir .Example: -SqlFiles "\.sql$" |
|
OutputPath | If set, indicates the single text file to which all script outputs will be written. Relative paths will be resolved relative to the current directory. File will be overwritten if it exists. Example: -OutputPath "Result.txt" |
|
OutputTable | Switch parameter. If present, causes output data to be formatted as a table. Otherwise output data is formatted as a list. Example: -OutputTable |