【发布时间】:2016-01-01 21:15:57
【问题描述】:
我不明白如何使用 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 文件并删除密码。该代码按以下方式工作。
- 包含扩展名的文档的完整文件路径由技术人员输入。
- 代码测试文档的路径和扩展名,以确保两者都有效。
- 它将文档复制到公用文件夹并将扩展名更改为 .zip。
- 然后解压缩文件。
- 解压后获取Word文件夹下的settings.xml文件内容。
- 这是我遇到问题的地方。我的目标是从 settings.xml 文件中删除密码。
- 删除密码后,我需要重新压缩所有解压缩的文件并将扩展名更改回 .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>
【问题讨论】:
标签: xml powershell openxml