我正在尝试使用PowerShell编辑现有的XML文件,以通过指向此XML来设置与组策略的文件关联。我需要在这个XML中动态添加或删除条目。XML如下所示:
<?xml version="1.0" encoding="utf-8"?>
<DefaultAssociations>
<Association Identifier=".pdf" ProgId="Acrobat.Document.2017" ApplicationName="Adobe Acrobat 2017" />
<Association Identifier=".acrobatsecuritysettings" ProgID="Acrobat.acrobatsecuritysettings" ApplicationName="Adobe Acrobat 2017" />
<Association Identifier="acrobat" ProgId="acrobat" ApplicationName="Adobe Acrobat 2017" />
</DefaultAssociations>在PowerShell中,我试图涵盖以下条件: XML文件和条目存在,XML文件存在但没有Acrobat的条目,XML和条目存在:
$XMLPath = "C:\Program Files (x86)\scripts\defaultassociations.xml"
$PackageName = 'defaultassociations'
$XMLContent = Get-Content -Path $XMLpath
$XMLContentFound1 = $XMLContent | Select-String -pattern "Acrobat.Document.2017"
$XMLContentFound2 = $XMLContent | Select-String -pattern "Acrobat.acrobatsecuritysettings"
$XMLContentFound3 = $XMLContent | Select-String -pattern '(Identifier="acrobat")'
$AcrobatAssociation = ($XMLContentFound1 -and $XMLContentFound2 -and $XMLContentFound3)
#XML exsists, Entry exsists
If($AcrobatAssociation) {
Write-Host "Acrobat association set"
}
#XML exsists, Entry DOES NOT exsists
If(!$AcrobatAssociation){
$xml=[xml](get-content $XMLPath)
}
#XML does not exsits
If(!(Test-Path -Path "$XMLPath")){
#set encoding
$encoding = [System.Text.Encoding]::UTF8
$xmlWriter = New-Object System.XMl.XmlTextWriter($XMLPath,$encoding)
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement('DefaultAssociations')
$xmlWriter.WriteStartElement('Association Identifier=".pdf" ProgId="Acrobat.Document.2017" ApplicationName="Adobe Acrobat 2017"')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteStartElement('Association Identifier=".acrobatsecuritysettings" ProgID="Acrobat.acrobatsecuritysettings" ApplicationName="Adobe Acrobat 2017"')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteStartElement('Association Identifier="acrobat" ProgId="acrobat" ApplicationName="Adobe Acrobat 2017"')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
}
#Remove entry
If($AcrobatAssociation)
-> remove the entry
<Association Identifier=".pdf" ProgId="Acrobat.Document.2017" ApplicationName="Adobe Acrobat 2017" />
<Association Identifier=".acrobatsecuritysettings" ProgID="Acrobat.acrobatsecuritysettings" ApplicationName="Adobe Acrobat 2017" />
<Association Identifier="acrobat" ProgId="acrobat" ApplicationName="Adobe Acrobat 2017" />有什么想法吗?我该如何操作XML来实现这一点呢?谢谢!
发布于 2018-06-15 00:04:20
我可能会替换下面的代码片段...
$XMLContentFound1 = $XMLContent | Select-String -pattern "Acrobat.Document.2017"就像这样……
$xmlNode_DefaultAssoc = $xmldoc.DefaultAssociations.Association
if ( $($xmlNode_DefaultAssoc.ProgId | % { $_ -eq 'Acrobat.Document.2017' }) ) {
$XMLContentFound1 = $true
}我不会像你那样创建一个xmlWriter,我可能会用下面这样的方式邮寄它……
[xml]$xmlDoc = @"
<?xml version="1.0" encoding="utf-8"?>
<DefaultAssociations>
<Association Identifier=".pdf" ProgId="Acrobat.Document.2017" ApplicationName="Adobe Acrobat 2017" />
<Association Identifier=".acrobatsecuritysettings" ProgID="Acrobat.acrobatsecuritysettings" ApplicationName="Adobe Acrobat 2017" />
<Association Identifier="acrobat" ProgId="acrobat" ApplicationName="Adobe Acrobat 2017" />
</DefaultAssociations>
"@为了澄清,我并不是通过XML DOM调用来构建文档。我只是创建了一个很大的字符串,并将其转换为XML。
要删除一个元素,我会使用类似下面这样的代码...
$xmlNode = $xmlDoc.DefaultAssociations.SelectSingleNode("Association[@ProgId='Acrobat.Document.2017']")
$xmlDoc.DefaultAssociations.RemoveChild($xmlNode)您也可以将此方法扩展到其他元素。
https://stackoverflow.com/questions/50858096
复制相似问题