1

我是PowerShell的新手。我正在尝试将我们的一些数据库脚本转换为 powershell 脚本。我们目前在脚本中做的一件事(DOS BATCH FILE)是使用 Type 命令...

@ECHO OFF

DEL *.sql 1>NUL 2>&1

TYPE ..\db\database\TuscanyProfileDB.sql > CreateDB.sql
TYPE ..\db\tbls\*.sql > CreateTables.sql
TYPE ..\db\foreignKeys\*.sql > CreateForeignKeys.sql
TYPE ..\db\indexes\*.sql > CreateIndexes.sql
TYPE ..\db\sprocs\*.sql > CreateSprocs.sql

它基本上进入指定的文件夹,并将所有具有.sql文件扩展名的文件连接起来,并将它们组合成一个新文件。

我的问题是如何在 powershell 中执行此操作?

4

2 回答 2

3
Remove-Item *.sql

Get-Content ..\db\database\TuscanyProfileDB.sql | Add-Content CreateDB.sql
Get-Content ..\db\tbls\*.sql | Add-Content CreateTables.sql
Get-Content ..\db\foreignKeys\*.sql | Add-Content CreateForeignKeys.sql
Get-Content ..\db\indexes\*.sql | Add-Content CreateIndexes.sql
Get-Content ..\db\sprocs\*.sql | Add-Content CreateSprocs.sql
于 2011-04-18T23:15:26.267 回答
0

这是我使用的,改编自这里

就我而言,我倾向于有很多目录,每个目录都有很多 SQL 文件,我想为每个目录创建一个单独的合并 SQL 文件。我只想合并以数字为前缀的文件(例如001_blah.sql),而我的合并文件被命名为 upgrade_{DIRECTORY}.sql (例如merged_1370_to_1380.sql)。

我将merge_sql_files.ps1文件放在父目录中,并merge.bat在每个目录中放置一个带有 SQL 文件的文件。

merge.bat文件看起来像这样

@ECHO OFF
SET PsFile=merge_sql_files.ps1
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%..\%PsFile%

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

merge_sql_files.ps1看起来像这样:

# Script to merge SQL files together. 
# Takes all the SQL files that start with a number and concats 
# them together, putting a line with "GO" in betweeen each one. 
# The output is saved as upgrade_[DirectoryName].sql, e.g. 
# 'upgrade_1370_to_1380.sql'. 
#
# This script should be run from the folder with the sql files, 
# e.g. put a merge.bat file in the folder that runs this ps1 script.

 
$path = (Resolve-Path .\).Path
$dirName = (get-item $path).Name
$outFile = "${path}\upgrade_${dirName}.sql"
 
if((Test-Path $outFile) -eq $true) {Remove-Item -Path $outFile -Force}
 
# Get all the SQL files that start with a number. Sort them 
$files = Get-ChildItem -LiteralPath $path -Include "*.sql" | where {$_.Name -match "^[0-9]" } | Sort-Object -Property Name
 
New-Item -ItemType file -Path $outFile -Force | Out-Null
 
foreach($file in $files)
{
    Write-Host "Appending file $file..." -ForegroundColor Gray
    $content = Get-Content -Path $file.FullName
    Add-Content -Path $outFile "----------------------------------------------------------------------------------------------------------------------------------------------------------------"
    Add-Content -Path $outFile "--      $File"
    Add-Content -Path $outFile "----------------------------------------------------------------------------------------------------------------------------------------------------------------"
    Add-Content -Path $outFile $content
    Add-Content -Path $outFile "GO`r`n"
}
 
Write-Host "Completed file $outFile" -ForegroundColor DarkGreen

# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost")
{
    Write-Host "Press any key to continue..."
    $Host.UI.RawUI.FlushInputBuffer()   # Make sure buffered input doesn't "press a key" and skip the ReadKey().
    $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}
于 2022-02-24T14:47:17.523 回答