我已经使用哈希表修改了XML中的每个StartOrder和StopOrder值-调试时可以很好地看到它。现在,当我尝试保存XML时,所有旧值都在其中,而没有新值。
因此,正如我所说-我修改的值非常好-在ISE中可以看到。尽管.Save函数几乎只是将“旧” XML复制到“新” XML。
Powershell:
$path = Get-Location
$configXML = Get-ChildItem -Path $path -Filter ManageSQLJobs.xml -File -Recurse
$script:xmldoc = New-Object Xml
[xml]$oofXML = Get-Content $configXML.FullName
$jobNames = $oofXML.Configuration.Servers.Server.Jobs.Job |
select Name, StartOrder, StopOrder
$hashTable = @{
BackupFullJob=30
#showing just one job here otherwise there's a lot more
}
foreach ($key in $hashTable.Keys) {
$message = 'Job name: {0}, value: {1}.' -f $key, $hashTable[$key]
Write-Output $message
foreach ($job in $jobNames) {
echo "Job name: " $job
echo "Start order: " $startOrder
echo "Stop order: " $stopOrder
if ($job.Name -eq $key) {
$job.StartOrder = $job.StartOrder.Replace($job.StartOrder, $hashTable[$key])
$job.StopOrder = $job.StopOrder.Replace($job.StopOrder, $hashTable[$key])
}
}
}
$job.Name
$job.StartOrder
$job.StopOrder
$oofXML.Save("C:\Users\janb\Desktop\NewXML.xml")
示例XML:
<?xml version="1.0"?>
<Configuration>
<Description>redacted</Description>
<Servers>
<Server Instance="redacted">
<Jobs>
<Job Name="BackupFullJob">
<StartOrder>1</StartOrder>
<StopOrder>1</StopOrder>
<Enable>1</Enable>
<Disable>1</Disable>
<Stop>0</Stop>
<Start>0</Start>
<WaitToFinishAtDisable>1</WaitToFinishAtDisable>
<WaitToFinishAtEnable>0</WaitToFinishAtEnable>
</Job>
</Jobs>
</Server>
</Servers>
</Configuration>
我希望XML内的StartOrder和StopOrder值将被哈希表中的值更新,但它们保持不变。 调试时,即使在脚本末尾,我也可以看到值正在更新-但是,当我保存XML时,旧值保留在那里,而不是新值。
答案 0 :(得分:0)
此声明
$jobNames = $oofXML.Configuration.Servers.Server.Jobs.Job |
select Name, StartOrder, StopOrder
从<Job>
节点创建自定义对象,这些对象与XML数据结构分离。您所做的更改将应用于这些对象,但不会回到XML数据结构中。
删除select
语句
$jobNames = $oofXML.Configuration.Servers.Server.Jobs.Job
问题将消失。