【发布时间】:2015-07-07 15:53:23
【问题描述】:
我正在尝试使用 powershell 脚本比较 web.config 文件从生产到非生产。我能够生成最多可以比较一个级别的代码,但除此之外,我没有在互联网上找到任何帮助来比较和更新具有多级深度节点结构的 xml 文件。下面是我的代码,它可以在文件的第一级工作,但不能超过。有什么方法可以比较两个文件并更新而不对任何节点元素进行硬编码?下面是我的代码,它可以工作到一个级别。
$src_web_config = "C:\Users\live\web.config"
$dst_web_config = "C:\Users\int\web.config"
# Creating XML objects for live and integration
$src_xml = New-Object -TypeName XML
$dst_xml = New-Object -TypeName XML
$src_xml.Load($src_web_config)
$dst_xml.Load($dst_web_config)
# get top nodes
$src_top_node = $src_xml.DocumentElement.Name
$dst_top_node = $dst_xml.DocumentElement.Name
function add_node($add_node)
{
Write-Host "Node", $add_node.name, "doesn't exist"
$node = $dst_xml.importnode($add_node, $true)
$dst_xml.$dst_top_node.appendchild($node)
$dst_xml.Save($dst_web_config)
}
if ($src_top_node -eq $dst_top_node)
{
if ($src_xml.$src_top_node.HasChildNodes)
{
$src_xml.$src_top_node.ChildNodes | % {
if ($dst_xml.$dst_top_node.ChildNodes.name -contains $_.name)
{
if ($_.haschildnodes)
{
#nothing I can find
}
}
else
{
add_node($_)
}
}
}
}
问候, Vj
【问题讨论】:
标签: powershell-4.0