不要通过PowerShell命令
问题描述:
我有一个要求运行时节点下的条目的XML文件(以红色突出显示)添加相同的条目上的xml:不要通过PowerShell命令
我能够通过以下PowerShell来实现这一目标脚本
$appFrameContainerAppConfig = 'D:\H.Infrastructure.AppFrame.Container.exe.config'
$doc = (Get-Content $appFrameContainerAppConfig) -as [Xml]
$root = $doc.get_DocumentElement()
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","EntityFramework")
$newAssemblyIdentity.SetAttribute("publicKeyToken","b77a5c561934e089")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","6.0.0.0")
$newCodeBase.SetAttribute("href","EntityFramework_6.1.3/EntityFramework.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","EntityFramework.SqlServer")
$newAssemblyIdentity.SetAttribute("publicKeyToken","b77a5c561934e089")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","6.0.0.0")
$newCodeBase.SetAttribute("href","EntityFramework_6.1.3/EntityFramework.SqlServer.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","System.Data.SQLite")
$newAssemblyIdentity.SetAttribute("publicKeyToken","db937bc2d44ff139")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","1.0.98.0")
$newCodeBase.SetAttribute("href","SQLite_1.0.98.1/System.Data.SQLite.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","System.Data.SQLite.EF6")
$newAssemblyIdentity.SetAttribute("publicKeyToken","db937bc2d44ff139")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","1.0.98.0")
$newCodeBase.SetAttribute("href","SQLite_1.0.98.1/System.Data.SQLite.EF6.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","System.Data.SQLite.Linq")
$newAssemblyIdentity.SetAttribute("publicKeyToken","db937bc2d44ff139")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","1.0.98.0")
$newCodeBase.SetAttribute("href","SQLite_1.0.98.1/System.Data.SQLite.Linq.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
Write-Output $root.runtime.assemblyBinding.dependentAssembly
$doc.Save($appFrameContainerAppConfig)
我需要其评估如果条目以红色突出显示存在于XML若然避免添加相同的条件。
if($root.SelectNodes('//configuration/runtime/assemblyBinding/dependentAssembly/assemblyIdentity[@name="EntityFramework"]')) {
} else {
}
我在这里使用的条件似乎不起作用。它不会进入内部,如果或条件。有什么建议么?
答
检查<runtime>
节点是否已包含条目,例如,像这样:
if ($root.SelectNodes('//runtime/dependentAssembly').Count -eq 0) {
...
}
如果您需要更细粒度的检查,请调整XPath expression。
编辑:显然你的XML数据使用的命名空间,所以你需要有一个命名空间选择节点命名空间管理:
$nsm = New-Object System.Xml.XmlNamespaceManager($doc.NameTable)
$nsm.AddNamespace('ns', 'urn:schemas-microsoft-com:asm.v1')
if ($doc.SelectNodes('//ns:assemblyBinding/dependentAssembly/assemblyIdentity[@name="EntityFramework"]', $nsm).Count -eq 0) {
...
}
这不工作,我仍然最终以创建重复的条目。事实上它根本不会进入if循环 –
“不工作”是一个不充分的问题描述。请编辑你的问题并提供证据。 –
我的意思是if循环没有得到执行,我写了一些写入输出,如果循环没有执行 –