Skip to content

Instantly share code, notes, and snippets.

@kenzauros
Last active Feb 14, 2022
Embed
What would you like to do?
ディレクトリ内の SQL ファイルを結合する PowerShell スクリプト (SQL Server 用)
Param(
[parameter(mandatory)][String]$outputFile
)
$divider="-- $("=" * 100)"
$files = Get-ChildItem .\*.* -Include *.sql -Exclude $outputFile
if (Test-Path $outputFile) { Clear-Content $outputFile }
function output {
process {
Out-File -Encoding UTF8 -FilePath $outputFile -Append -InputObject $PSItem
}
}
foreach ($file in $files) {
Write-Output $divider | output
Write-Output "-- $($file.Name)" | output
Write-Output $divider | output
Get-Content $file.FullName -Encoding UTF8 `
| Select-String -Pattern "^(BEGIN TRAN|ROLLBACK)" -NotMatch `
| Select-Object -ExpandProperty line | output
Write-Output "GO`r`n" | output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment