使用powershell设置与XML的文件关联

使用powershell设置与XML的文件关联,xml,powershell,Xml,Powershell,我正在尝试使用PowerShell编辑现有的XML文件,以通过指向此XML来设置与组策略的文件关联。我需要动态地从这个XML中添加或删除条目。XML如下所示: <?xml version="1.0" encoding="utf-8"?> <DefaultAssociations> <Association Identifier=".pdf" ProgId="Acrobat.Document.2017" ApplicationName="Adobe Acrobat 2

我正在尝试使用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" />
$XMLPath=“C:\ProgramFiles(x86)\scripts\defaultassociations.xml”
$PackageName='defaultassociations'
$XMLContent=获取内容-路径$XMLpath
$XMLContentFound1=$XMLContent |选择字符串模式“Acrobat.Document.2017”
$XMLContentFound2=$XMLContent |选择字符串模式“Acrobat.acrobatsecuritysettings”
$XMLContentFound3=$XMLContent |选择字符串模式’(Identifier=“acrobat”)'
$AcrobatAssociation=($XMLContentFound1-和$XMLContentFound2-和$XMLContentFound3)
#XML存在,条目存在
国际单项体育联合会{
写入主机“Acrobat关联集”
}
#XML存在,条目不存在
如果(!$AcrobatAssociation){
$xml=[xml](获取内容$XMLPath)
}
#XML不存在
If(!(测试路径-路径“$XMLPath”)){
#集合编码
$encoding=[System.Text.encoding]::UTF8
$xmlWriter=newobjectsystem.XMl.XmlTextWriter($XMLPath,$encoding)
$xmlWriter.Formatting='Indented'
$xmlWriter.Indentation=1
$XmlWriter.IndentChar=“`t”
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStarteElement('DefaultAssociations')
$xmlWriter.WriteStarteElement('Association Identifier=“.pdf”ProgId=“Acrobat.Document.2017”ApplicationName=“Adobe Acrobat 2017”'))
$xmlWriter.WriteEndElement()
$xmlWriter.WriteStarteElement('Association Identifier=“.acrobatsecuritysettings”ProgID=“Acrobat.acrobatsecuritysettings”ApplicationName=“Adobe Acrobat 2017””)
$xmlWriter.WriteEndElement()
$xmlWriter.WriteStarteElement('Association Identifier=“acrobat”ProgId=“acrobat”ApplicationName=“Adobe acrobat 2017”'))
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
}
#删除条目
国际单项体育联合会
->删除条目

有什么想法可以让我操作XML来实现这一点吗?谢谢

我可能会替换以下代码段

$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>
"@
$xmlNode = $xmlDoc.DefaultAssociations.SelectSingleNode("Association[@ProgId='Acrobat.Document.2017']")
$xmlDoc.DefaultAssociations.RemoveChild($xmlNode)

您也可以将此方法扩展到其他元素。

谢谢adam!使用“$xmlNode_DefaultAssoc”查找节点有什么好处。到目前为止,我还不太熟悉PS的整个世界。使用字符串创建XML是一个好主意,现在我想知道,因为这会覆盖任何现有内容,我如何在表中添加这些条目(它是表吗?!)获取文档内容并执行选择字符串(grep样式的搜索),将在文档中的任何位置找到该字符串。如果值在您希望的位置,则检查元素的属性(使用
$xmlNode\u DefaultAssoc…
)将返回true。