Я довольно новый с 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
и отклоняет их безуспешно.