我有XML配置文件,定义如下
<?xml version="1.0"?>
<Scripts>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "1" sql = "PreDeployment_265015_Add_column_TotalAmount.sql"/>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "2" sql = "PreDeployment_265375_Add_column_IsPassFloorViolaton.sql"/>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "3" sql = "PreDeployment_265915_Add_Columns_InferredMemberSource.sql"/>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "1" sql = "Predeployment_266364_AddPSOKeyAndOffsrpingDirectorCount.sql"/>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "1" sql = "PreDeployment_267185_DropTheOLdRelationshiphistoryTable.sql"/>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "1" sql = "PreDeployment_268731_Add_Column_TaxExemptStatus_to_DimOrder.sql"/>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "1" sql = "AM_QA_Contacts.sql"/>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "1" sql = "QA_Intelligence_AM.sql"/>
<Script Server = ".\" DBName = "Intelligence_Audit_Local" Sequence = "1" sql = "AuditScripts.sql"/>
<Script Server = ".\" DBName = "Intelligence_FO_Local" Sequence = "1" sql = "AuditScripts.sql"/>
</Scripts>
我需要更改powershell脚本,使其运行XML中提到的所有sql文件,这些文件具有序列1,然后运行所有具有序列2的文件,依此类推。因此,序列2脚本只应在序列1完成后运行。目前,以下脚本仅针对序列1执行。
[xml]$xml = [xml] (Get-Content -Path $configfile)
$Scripts= $xml.SelectNodes("/Scripts/Script")
foreach ($Script in $Scripts | where {$_.Sequence -eq "1"} ) {
$sqlfilename = $Script.getAttribute("sql")
$Server = $Script.getAttribute("Server")
$DBName = $Script.getAttribute("DBName")
$filename = $sqlfilename.Replace(".sql", "")
$ps1 = $location + "\" + $sqlfilename.Trim()
$ErrorOutputFile = "Error_output_{0}_{1}.txt" -f $filename.Trim(), $OutputDate
If (Test-Path -path $ps1) {
add-content $logfile "- Started: $ps1 "
$commandLine = "Invoke-Sqlcmd -ServerInstance $Server -Database $DBName -InputFile $ps1"
Start-Process "powershell.exe" -ArgumentList $commandLine -RedirectStandardError "$Outputlocation\$ErrorOutputFile" #-ErrorAction silentlycontinue
add-content $logfile "----------------------------------------"
$i++;
}
Else {
add-content $logfile "- File not found: $ps1 "
add-content $logfile "----------------------------------------"
$x++;
}
}
答案 0 :(得分:1)
尝试这样的事情:
$configfile="C:\temp\config.xml"
$logfile="C:\temp\Log.txt"
[xml]$xml = [xml] (Get-Content -Path $configfile)
$xml.SelectNodes("/Scripts/Script") | sort Sequence | foreach {
$sqlfilename = $_.Sql.Trim()
$ps1 = "$location\$sqlfilename"
if (!(Test-Path -path $ps1))
{
add-content $logfile "- File not found: $ps1 "
add-content $logfile "----------------------------------------"
$x++
continue
}
$filename = $sqlfilename.Replace(".sql", "").Trim()
$ErrorOutputFile = "Error_output_{0}_{1}.txt" -f $filename, $OutputDate
add-content $logfile "- Started: $ps1 "
$commandLine = "Invoke-Sqlcmd -ServerInstance {0} -Database {1} -InputFile {2}" -f $_.Server, $_.DBName, $ps1
Start-Process "powershell.exe" -ArgumentList $commandLine -RedirectStandardError "$Outputlocation\$ErrorOutputFile" #-ErrorAction silentlycontinue
add-content $logfile "----------------------------------------"
$i++;
}
说明:
我按顺序排序并删除where子句。 我没有义务重组,只是优化