PowerShell脚本更新XML节点值二次执行失效问题求助
问题:PowerShell XML更新脚本二次执行失效
我编写了如下PowerShell脚本,用于更新C:\tmp\unzipped\文件夹中多个XML文件的UnitholderId节点值:首次执行脚本时,UnitholderId节点值成功更新为987654,但第二次执行时脚本不再生效,单独处理单个文件也存在相同问题。我是PowerShell脚本新手,不确定遗漏了什么,恳请协助排查。
原PowerShell脚本
Get-ChildItem C:\tmp\unzipped\ *.xml | ForEach-Object { $xml = [xml](Get-Content $_) $node = $xml.SelectNodes("//RecipientInfoList//UnitholderList") $node1 = $xml.SelectNodes("//RecipientInfoList//UnitholderList//List") foreach ($nodes in $node) { $NodeExists = $nodes.UnitholderId if (($nodes.UnitholderId = Null) || $NodeExists) { $nodes.UnitholderId = '987654' } } foreach ($nodess in $node1) { $NodeExists = $nodes.UnitholderId if (($nodes.UnitholderId = Null) || $NodeExists) { $nodess.UnitholderId = '987654' } } $xml.Save($_) }
原XML文件内容
<DocumentDelivery> <Document> <Recipient> <RecipientType>INVESTOR COPY</RecipientType> <RecipientId>Test123456</RecipientId> <RecipientName>TestNameABC</RecipientName> <RecipientAddress1>TestAddress 1</RecipientAddress1> <RecipientAddress2>TestAddress 2</RecipientAddress2> <RecipientAddress3>TestAddress 3</RecipientAddress3> <RecipientAddress4> </RecipientAddress4> <RecipientZipcode>Lb-6012</RecipientZipcode> <RecipientCountryCode>GBR</RecipientCountryCode> <RecipientCountry>UNITED KINGDOM</RecipientCountry> <RecipientFaxNumber>123456</RecipientFaxNumber> <RecipientEmail>test@abc.com</RecipientEmail> <RecipientContactPerson> </RecipientContactPerson> </Recipient> <RecipientInfoList> <UnitholderList> <UnitholderId>123456</UnitholderId> <List> <UnitholderId>123456</UnitholderId> </List> </UnitholderList> </RecipientInfoList> </Document> </DocumentDelivery>
首次执行后的XML文件内容
<DocumentDelivery> <Document> <Recipient> <RecipientType>INVESTOR COPY</RecipientType> <RecipientId>Test123456</RecipientId> <RecipientName>TestNameABC</RecipientName> <RecipientAddress1>TestAddress 1</RecipientAddress1> <RecipientAddress2>TestAddress 2</RecipientAddress2> <RecipientAddress3>TestAddress 3</RecipientAddress3> <RecipientAddress4> </RecipientAddress4> <RecipientZipcode>Lb-6012</RecipientZipcode> <RecipientCountryCode>GBR</RecipientCountryCode> <RecipientCountry>UNITED KINGDOM</RecipientCountry> <RecipientFaxNumber>123456</RecipientFaxNumber> <RecipientEmail>test@abc.com</RecipientEmail> <RecipientContactPerson> </RecipientContactPerson> </Recipient> <RecipientInfoList> <UnitholderList> <UnitholderId>987654</UnitholderId> <List> <UnitholderId>987654</UnitholderId> </List> </UnitholderList> </RecipientInfoList> </Document> </DocumentDelivery>
问题排查与修复
问题根源
- 赋值与比较混淆:脚本中用了
$nodes.UnitholderId = Null,这是赋值操作而非判断是否为Null的比较操作(正确写法是-eq $null),导致逻辑完全错误。 - 变量引用错误:第二个
foreach循环里错误引用了外层循环的$nodes变量,应该使用当前循环的$nodess。 - 冗余判断逻辑:试图判断节点是否存在或为空,但实际上可以直接选中所有
UnitholderId节点统一赋值,无需复杂判断。
修复后的脚本
Get-ChildItem C:\tmp\unzipped\ *.xml | ForEach-Object { $xml = [xml](Get-Content $_) # 直接选中所有UnitholderId节点 $allUnitholderIds = $xml.SelectNodes("//UnitholderId") foreach ($idNode in $allUnitholderIds) { $idNode.InnerText = '987654' } $xml.Save($_) }
说明
- 用XPath表达式
//UnitholderId可直接选中XML中所有UnitholderId节点,不受层级限制。 - 直接设置节点的
InnerText为目标值,无论原节点值是什么,重复执行都会生效。 - 脚本逻辑更简洁,彻底避免了原脚本的变量引用错误和逻辑混乱。
内容的提问来源于stack exchange,提问作者user21418581
相关产品推荐
相关产品推荐

