2016-08-22 161 views
1

我有一個要求運行時節點下的條目的XML文件(以紅色突出顯示)添加相同的條目上的xml:不要通過PowerShell命令

XMLfile

我能夠通過以下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 { 
} 

我在這裏使用的條件似乎不起作用。它不會進入內部,如果或條件。有什麼建議麼?

回答

0

檢查<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) { 
    ... 
} 
+0

這不工作,我仍然最終以創建重複的條目。事實上它根本不會進入if循環 –

+0

「不工作」是一個不充分的問題描述。請編輯你的問題並提供證據。 –

+0

我的意思是if循環沒有得到執行,我寫了一些寫入輸出,如果循環沒有執行 –