如何使用powershell批量拆分文件?
我有一个包含多个批次的 sql 部署后文件。下面是我的 postDeployment.sql 文件的示例。
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'StoredProc') AND type in (N'P', N'PC'))
BEGIN
DROP PROCEDURE [dbo].[StoredProc]
END
** GO **
CREATE PROCEDURE [dbo].[StoredProc]
@nodeId int = 0
AS
BEGIN
SET NOCOUNT ON;
END
**GO**
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'StoredProc') AND type in (N'P', N'PC'))
BEGIN
DROP PROCEDURE [dbo].[StoredProc]
END
**GO**
CREATE PROCEDURE [dbo].[StoredProc]
@nodeId int = 0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
END
$_
我需要自己执行每个批次。Shile 分裂突出显示 ** GO **
我的 PS 命令如下(无法正常工作):
function GetSqlBatchesFromString($file)
{
$buffer = new-object System.Text.StringBuilder
switch -regex -file $file {
"^\s*GO[\s\d]*$"
{
# $__ returns go
$buffer.ToString();
$buffer.Length = 0;
}
default
{
$temp = $buffer.AppendLine($_);
}
}
$buffer.ToString();
}
function ExecuteBatch($batch)
{
Write-Host "Batch part ==== start"
Write-Host $batch
Write-Host "Batch part ==== end"
}
GetSqlBatchesFromString "D:\PostDeployment.sql" | % { ExecuteBatch $__}