Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to delete an element from XML embedded in WMI

$
0
0

I have an xml embedded in WMI that contains the following.

<?xml version="1.0" encoding="utf-16"?><UpdateXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="SMS_SoftwareUpdate" LocaleId="1033"><UpdateXMLDescriptionItems><UpdateXMLDescriptionItem PropertyName="ArticleID" UIPropertyName=""><MatchRules><string>KB123456</string><string>KB121337</string><string>KB121338</string><string>KB121339</string><string>KB121334</string><string>KB121331</string><string>KB121330</string><string>KB1213333</string><string>KB12133799</string><string>KB121337</string></MatchRules></UpdateXMLDescriptionItem><UpdateXMLDescriptionItem PropertyName="UpdateLocales" UIPropertyName=""><MatchRules><string>'Locale:9'</string></MatchRules></UpdateXMLDescriptionItem><UpdateXMLDescriptionItem PropertyName="_Product" UIPropertyName=""><MatchRules><string>'Product:b86cf33d-92ac-43d2-886b-be8a12f81ee1'</string><string>'Product:83aed513-c42d-4f94-b4dc-f2670973902d'</string><string>'Product:00b2d754-4512-4278-b50b-d073efb27f37'</string><string>'Product:c755e211-dc2b-45a7-be72-0bdc9015a63b'</string></MatchRules></UpdateXMLDescriptionItem><UpdateXMLDescriptionItem PropertyName="IsSuperseded" UIPropertyName=""><MatchRules><string>false</string></MatchRules></UpdateXMLDescriptionItem><UpdateXMLDescriptionItem PropertyName="_UpdateClassification" UIPropertyName=""><MatchRules><string>'UpdateClassification:0fa1201d-4330-4fa8-8ae9-b877473b6441'</string></MatchRules></UpdateXMLDescriptionItem><UpdateXMLDescriptionItem PropertyName="_Company" UIPropertyName=""><MatchRules><string>'Company:56309036-4c77-4dd9-951a-99ee9c246a94'</string></MatchRules></UpdateXMLDescriptionItem></UpdateXMLDescriptionItems></UpdateXML>

I have a text file that contains a list of KB numbers that I want to delete from the above XML

Text File contains;

KB13579
KB121334
KB121331

How can I delete the <string> elements from the above XML that match the KB numbers in the text file

 

I 'm using the below but it is not working giving this error at the bold section of the code.

The error is:

Cannot convert argument "0", with value: "KB13579", for "RemoveChild" to type "System.Xml.XmlNode": "Cannot convert
the "KB13579" value of type "System.String" to type "System.Xml.XmlNode"."
At C:\temp\pstest3.ps1:32 char:29
+                             ($xml.SelectSingleNode("/UpdateXML/UpdateXMLDescript ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

 

POWERSHELL CODE BELOW:

Import-Module "E:\SMS\AdminConsole\bin\ConfigurationManager.psd1" 
$SiteCode = "CAS"
#"KB121337"
$ADRTarget = "TESTADR"
write-output $ADRTarget
$IncludeKBs = (Get-Content C:\temp\KBArticle.txt)
$adrs = Get-CMSoftwareUpdateAutoDeploymentRule  
foreach ($adr in $adrs) 
   {if ($adr.name -eq $ADRTarget) 
       {$xml = New-Object XML  
        $xml.LoadXml($adr.UpdateRuleXML)  
        $ADRkbs = $xml.selectnodes("/UpdateXML/UpdateXMLDescriptionItems/UpdateXMLDescriptionItem[@PropertyName='ArticleID']")
            foreach ($UpdateXMLDescriptionItemNode in $ADRKBs) 
                {$stringNodeList = $UpdateXMLDescriptionItemNode.selectNodes("MatchRules/string")}
 		#Delete KB's in KBlist from ADR so we start fresh
            	Write-Output "We will now test if we have existing KB's in ADR and delete if found"
            	foreach ($includekb in $includekbs) 
                    {Write-Output "Now we are going to delete this Kb: " $IncludeKB
                    foreach ($stringNode in $stringNodeList)
                        {If ($stringnode.get_innerXml() -eq $IncludeKB)
                           {Write-Output "Found the KB will now delete: " $stringnode.get_innerXml() 
                            $xmlinput = $xml.CreateElement("string")  
                            $xmlinput.InnerText = $IncludeKB 
                            ($xml.SelectSingleNode("/UpdateXML/UpdateXMLDescriptionItems/UpdateXMLDescriptionItem[@PropertyName='ArticleID']")).RemoveChild($xmlinput) | Out-Null 
                         }
                    }               }



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>