Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the line <w:documentProtection ... /> in an xml file using Powershell?

I do not understand how to remove this line in an xml file using Powershell.

<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==" />

The objective is to take a password protected .docx file and remove the password. The code works in the following way.

  1. The full file path of the document including the extension is entered by the technician.
  2. The code tests the path and extension of the document to ensure both are valid.
  3. It copies the document to the public folder and changes the extension to .zip.
  4. It then unzips the file.
  5. After the unzip, it gets the content of the settings.xml file found under the Word folder.
  6. Here is where I am having issues. My goal is to remove the password from the settings.xml file.
  7. Once the password is removed, I need to re-zip all unzipped files and change the extension back to .docx.

Here is the Powershell script pasted below:

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."

    }



}

And here is the content of the xml file.

<?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>
like image 552
Rob Avatar asked May 06 '26 02:05

Rob


1 Answers

You must reference and use the "http://schemas.openxmlformats.org/wordprocessingml/2006/main" namespace that <documentProtection> is a member of:

$xml `
| Select-Xml -XPath '//w:documentProtection' -Namespace @{w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"} `
| Foreach{$_.Node.ParentNode.RemoveChild($_.Node)}
like image 167
Tomalak Avatar answered May 09 '26 04:05

Tomalak