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

时间:2018-06-14 12:55:15

标签: 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 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来实现这一目标?谢谢!

1 个答案:

答案 0 :(得分:1)

我可能会替换以下代码段...

#!/usr/bin/tcsh bash
sed 's/"//g' rename_list.csv | while IFS=, read orig new num; do 
    mv "$orig" "$new"
done
echo '1 Done.'
for file in *.pdf; do
    mkdir "${file%.*}"
    mv "$file" "${file%.*}"
done
echo '2 done.'
sed 's/"//g' rename_list.csv | while IFS=, read orig new num; do 
    for folder in * ; do
        if [[ .* = "$num" ]]; then
            cp -R "$folder" "${file%.*}"
            else echo "No matches found."
        fi
    done
done
echo '3 Done.'

有这样的事情......

$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 DOM调用构建文档。我只是创建一个大字符串并将其转换为XML。

要删除元素,我会使用类似的内容......

[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>
"@

您也可以针对其他元素扩展此方法。