2016-10-17 3 views
0

Я довольно новый с PowerShell, и я написал следующий скрипт. Чтобы получить все задания с карты и изменить линию @command на новый стандарт.Фильтрация файлов SQL после замены

$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 [email protected] 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 @[email protected], 
     @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 @[email protected], @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 и no-match плюс второй цикл foreach, где он сравнивает все строки @command и отклоняет их безуспешно.

ответ

0

Привет я решить мою проблему, используя следующий код

$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) { 

    # $doelbestand = $fileser.name + '.goed' 

    $succes = "$submapchanged\" + $fileser.name #.replace("_oudeserver","_goed") 
    $failure = "$submapfailed\" + $fileser.name.replace("_goed","_fout") 

    $filename = $fileser.fullname 
    $content = Get-Content $filename 

#Replace servername if [email protected] in the sql job is '@command=N''/ISSERVER' 
    if ($content -match $commandline_new){ 
       $content = $content -replace $sourceserver, $newsqlserver} 

#Here the @command lines that are now '@command=N''/FILE' are being traformed to the '@command=N''/ISSERVER' standard 

    if ($content -match $commandline_old){ 
     $step1 =$content -replace "/CONFIGFILE.*\"" " 
     $step2 = $step1 -replace "/CHECKPOINTING OFF" , $stringtoreplace 
     $step3 = $step2 -replace """\\""D:.*dtsx", $tijdelijkeveranderingregeltest 
     $content = $step3 -replace $commandline_old, $commandline_new 

     } 

ForEach ($line in $content) 
{ 
    #write-host $line 
    if ($line.Contains($commandline)) 
    { 
    $index = $line.IndexOf($commandline) 
    $command = $line.Substring($index + 11, 9) 

    if ($command -eq '/ISSERVER') 
    { 
    #  write-host 'GOED!' 
     $content |out-file $succes 
    } 
    else 
    { 
      # write-host 'FOUT!' 

     $content |out-file $failure 
    } 
}} } 

#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 
    } 

я добавил ForEach ($line in $content) цикличным корыта все строки и проверить, если все '@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 
    } 
Смежные вопросы