我不明白如何使用Powershell在xml文件中删除此行。
<w:documentProtection w:edit="readOnly" w:formatting="1" w:enforcement="1"
w:cryptProviderType="rsaFull" w:cryptAlgorithmClass="hash"
w:cryptAlgorithmType="typeAny" w:cryptAlgorithmSid="4"
w:cryptSpinCount="100000" w:hash="FkH8Hm3kZbvQMc1//8dn96Z5qJc="
w:salt="NM8IjgZnzrC+sbqmM9JVxA==" />
目标是获取受密码保护的.docx文件并删除密码。代码以下列方式工作。
以下是粘贴的Powershell脚本:
function CrackIt
{
write-host = "Please enter the full file path including the extension. (example: C:\Users\Public\filname.docx)"
$filepath = read-host "Filepath "
$ResultTest = test-path $filepath
#gets the extension of the file
$extension = [System.IO.Path]::GetExtension($filepath)
#tests path
if(($ResultTest -eq $False))
{
write-host "Invalid Filepath"
}
else
{
if($extension -eq ".docx")
{
#copies file to public folder and changes extension to .zip
$newpath = Copy-Item -Path $filepath –Destination ([io.path]::ChangeExtension($filepath, '.zip')) -Verbose -PassThru -ErrorAction SilentlyContinue
Unzip "$newpath" "C:\Users\Public\Documents" #unzips the file
#Here's where I am trying to remove the password.
$File = 'C:\Users\Public\Documents\word\settings.xml'
[xml]$xml = Get-Content $File
$xml | Select-Xml -XPath '//w:documentProtection' | Foreach{$_.Node.ParentNode.RemoveChild($_.Node)}
$xml.Save($File)
else
{
write-host "File type not supported. If possible, please save the document in either a .docx or .xlsx format. If it is not possible, oh well."
}
}
以下是xml文件的内容。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:settings xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" mc:Ignorable="w14">
<w:zoom w:percent="100" />
<w:proofState w:spelling="clean" w:grammar="clean" />
<w:documentProtection w:edit="readOnly" w:formatting="1" w:enforcement="1" w:cryptProviderType="rsaFull" w:cryptAlgorithmClass="hash" w:cryptAlgorithmType="typeAny" w:cryptAlgorithmSid="4" w:cryptSpinCount="100000" w:hash="FkH8Hm3kZbvQMc1//8dn96Z5qJc=" w:salt="NM8IjgZnzrC+sbqmM9JVxA==" />
<w:defaultTabStop w:val="720" />
<w:characterSpacingControl w:val="doNotCompress" />
<w:compat>
<w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="14" />
<w:compatSetting w:name="overrideTableStyleFontSizeAndJustification" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
<w:compatSetting w:name="enableOpenTypeFeatures" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
<w:compatSetting w:name="doNotFlipMirrorIndents" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
</w:compat>
<w:rsids>
<w:rsidRoot w:val="009A3523" />
<w:rsid w:val="00185564" />
<w:rsid w:val="006A3026" />
<w:rsid w:val="009A3523" />
</w:rsids>
<m:mathPr>
<m:mathFont m:val="Cambria Math" />
<m:brkBin m:val="before" />
<m:brkBinSub m:val="--" />
<m:smallFrac m:val="0" />
<m:dispDef />
<m:lMargin m:val="0" />
<m:rMargin m:val="0" />
<m:defJc m:val="centerGroup" />
<m:wrapIndent m:val="1440" />
<m:intLim m:val="subSup" />
<m:naryLim m:val="undOvr" />
</m:mathPr>
<w:themeFontLang w:val="en-US" />
<w:clrSchemeMapping w:bg1="light1" w:t1="dark1" w:bg2="light2" w:t2="dark2" w:accent1="accent1" w:accent2="accent2" w:accent3="accent3" w:accent4="accent4" w:accent5="accent5" w:accent6="accent6" w:hyperlink="hyperlink" w:followedHyperlink="followedHyperlink" />
<w:shapeDefaults>
<o:shapedefaults v:ext="edit" spidmax="1026" />
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout>
</w:shapeDefaults>
<w:decimalSymbol w:val="." />
<w:listSeparator w:val="," />
</w:settings>
答案 0 :(得分:2)
您必须参考并使用&#34; http://schemas.openxmlformats.org/wordprocessingml/2006/main&#34; <documentProtection>
是其成员的名称空间:
$xml `
| Select-Xml -XPath '//w:documentProtection' -Namespace @{w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"} `
| Foreach{$_.Node.ParentNode.RemoveChild($_.Node)}
答案 1 :(得分:-1)
一种选择是将文件内容作为字符串加载,使用-replace运算符删除片段,然后转换为xml。类似的东西:
$xmlString = (Get-Content $File) -replace "<w:documentProtection.+/>`r`n", ""
$xml = [xml]''
$xml.LoadXml($xmlString)