PowerShell utility scripts for managing SQL script files

by Klaus Graefensteiner 29. June 2012 15:42

Introduction

This article contains a collection of useful PowerShell scripts for T-SQL developers.

BallValves

Figure 1: Ball valves and PVC pipes

Combine T-SQL script files

This script reads all files in a folder ending with .SQL and adds their content to one single file. This comes in handy, if you need to email the script text for example.

$Output = "C:\Users\KWG\Desktop\QualityLimitQueries.sql"

$Files = Get-ChildItem -Path "C:\Users\KWG\Desktop\New Quality Limit Tables" -Filter "*.sql"

Set-Content -Path $Output -Value "-- Klaus Graefensteiner $(Get-Date)"
foreach($File in $Files)
{
    Add-Content -Path $Output -Value "-- ---------------------------------------"
    Add-Content -Path $Output -Value "-- $($File.Name)"
    Add-Content -Path $Output -Value "-- ---------------------------------------"
    
    $Lines = Get-Content -Path $File.FullName
    foreach($Line in $Lines)
    {
        Add-Content -Path $Output -Value $Line
    }
}

Create Grant Execute script for create stored procedure files

This script looks for files ending in .SQL and starting with Create_sp. The file names contain the names of the stored procedures that they are going to create. The script takes the names and creates a new script for granting execute permissions to each of the stored procedure to a user called ReportAdmin.

$SQLScriptsFilePath = "C:\Documents and Settings\KlausG\My Documents\Reporting\SQL"
$InstallCommandsFilePath = Join-Path -Path $SQLScriptsFilePath -ChildPath "SQLGrantExecutes.txt"
$UserName = "ReportAdmin"


Set-Content -Path $InstallCommandsFilePath -Value "-- GRANT PERMISSIONS"
$SQLFiles = Get-ChildItem -Path $SQLScriptsFilePath  | Where-Object {$_.Name -match "^Create_sp.*?$"}

foreach($File in $SQLFiles)
{
    
    $LineTemplate = "GRANT EXECUTE ON {0} TO {1} GO" -f $File.Name, $UserName
    
    $Line = $LineTemplate
    $Line = $Line.Replace("Create_", "").Replace(".sql", "")
    Add-Content -Path $InstallCommandsFilePath -Value $Line

}

Create a SQLCMD batch file to install stored procedures

This script gets all the create stored procedure scripts in a folder and generates a batch file for SQLCMD.

$SQLScriptsFilePath = "C:\Documents and Settings\KlausG\My Documents\Reporting\SQL"
$InstallCommandsFilePath = Join-Path -Path $SQLScriptsFilePath -ChildPath "SQLSetupCommandLines.txt"


Set-Content -Path $InstallCommandsFilePath -Value "REM Commands"
$SQLFiles = Get-ChildItem -Path $SQLScriptsFilePath  | Where-Object {$_.Name -match "(^Create.*?$)|(^Insert.*?$)"}

foreach($File in $SQLFiles)
{
    
    $LineTemplate = "SQLCMD -S %1 -d %2 -E -i `"{0}`"" -f $File.Name
    $Line = $LineTemplate
    Add-Content -Path $InstallCommandsFilePath -Value $Line

}

Ausblick

As always PowerShell is a timesaver.

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2013, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2013 by Klaus Graefensteiner

Rendertime:
Page rendered at 5/19/2013 4:09:48 AM (PST Pacific Standard Time UTC DST -7)