替换后过滤SQL文件

时间:2016-10-17 09:15:23

标签: sql powershell

我是PowerShell的新手,我编写了以下脚本。从地图中获取所有作业并将@command行更改为新标准。

$sqlserver = 'currentserver'
$testservername = 'newcurrentserver'
$orgineel_submap = 'D:\Jobs_from_Server\Jobs_Orgineel_opdr_2.3'
$orgineel_submapnew ='D:\Jobs_from_Server\GUHQTSQL1281_TDBMSBI\Jobs_Orgineel_opdr_2.3\succes'
$orgineel_failure = 'D:\Jobs_from_Server\Jobs_Orgineel_opdr_2.3\failure'

$fileserver = Get-ChildItem $orgineel_submaporgineel *.sql 
$stringfile = '@command=N''/FILE'
$stringisserver = '@command=N''/ISSERVER'
$commandline = '@command=N'
$replaceline ="/Server " + """" + "\" + """" + $sqlserver + '\"" /Par "\"$ServerOption::LOGGING_LEVEL(Int16)\"";1 /Par "\"$ServerOption::SYNCHRONIZED(Boolean)\"";True /CALLERINFO SQLAGENT' 

foreach ($fileser in $fileserver) {
    $targetfile = "$orgineel_submapnew\" + $fileser.Name.Replace("_Orgineel", "_new")
    $rejected = "$orgineel_failure\" + $fileser.Name.Replace("_Orgineel","_failure")

    $filename = $fileser.FullName 
    $file = Get-Content $filename

    #Replace servername if the@command in the sql job is '@command=N''/ISSERVER'
    if ($file -match $stringisserver) {
        $file = $file -replace $testservername, $sqlserver
    }

    #Here the @command lines that are now '@command=N''/FILE' are being traformed to the '@command=N''/ISSERVER' standard
    if ($file -match $stringfile) {
        $stap1 = $file -replace "/CONFIGFILE.*\"" " 
        $stap2 = $stap1 -replace "/CHECKPOINTING OFF", $replaceline
        $file = $stap2 -replace $stringfile, $stringisserver
    } 

    #Here is checked that the file @command is '@command=N''/ISSERVER' and if this is the case it will bie aved in de Succes map
    if ($file -match $stringisserver) {
        $file | Out-File $targetfile
    } else {  #Here will the .sql jobs by saved if they are not '@command=N''/ISSERVER' 
       $file | Out-File $rejected
    }
} 

该脚本会更改所需的点,例如将'@command=N''/FILE'替换为'@command=N''/ISSERVER,如果这些作业不包含'@command=N''/ISSERVER,它也会过滤并将作业置于拒绝状态。

我的问题是foreach循环后的某些文件包含多个@command,例如:

EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, 
        @step_id=1, 
        @cmdexec_success_code=0, 
        @os_run_priority=0, @subsystem=N'SSIS', 
        @command=N'/ISSERVER "\"" /SERVER "\"GUVMOSRV65430\O_DBMS_A\""
        @database_name=N'master', 
        @flags=0
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Process, 
        @step_id=1, 
        @os_run_priority=0, @subsystem=N'SSIS', 
        @command=N'/FILE "\"" /SERVER "\"GUVMOSRV65430\O_DBMS_A\""
        @database_name=N'master', 
        @flags=0

我的问题是:如果包含@command=N'/ISSERVER的文件还包含另一个与@command不匹配的@command=N'/ISSERVER行,我如何确保其中包含-match的文件仍会被拒绝。

我尝试使用foreach和不匹配以及第二个@command循环来比较所有$(selector).css("attribut", ""); 行并拒绝但没有成功。

1 个答案:

答案 0 :(得分:0)

您好我已使用以下代码解决了我的问题

($line in $content)

我添加了一个ForEach '@command=N''/ISSERVER'来循环播放所有行并检查所有行是否#Removes the File die rejected is maar nog wel in de Job_aangepast staan $rejected = Get-ChildItem $submapfailed *.sql foreach ($reject in $rejected) { $fullname = $reject.fullname.replace("_fout", "_goed").replace("Jobs_Gefaald","Jobs_Aangepast") Remove-Item $fullname -erroraction silentlycontinue } 如果是这种情况,它会保存在地图成功中,如果不是它保存在地图中失败

如果文件也放在文件夹失败

中,则以下部分会从成功文件夹中删除该文件
s