Я не понимаю, как удалить эту строку в XML-файле, используя Powershell.Как удалить строку <w: documentProtection ... /> в xml-файле с помощью 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==" />
Целью является получение защищенного паролем файла .docx и удаление пароля. Код работает следующим образом.
- Полный путь к файлу документа, включая расширение, вводится техническим специалистом.
- Код проверяет путь и расширение документа для обеспечения того, чтобы оба значения были действительными.
- Он копирует документ в общую папку и изменяет расширение на .zip.
- Затем он распаковывает файл.
- После распаковки он получает содержимое файла settings.xml, найденного в папке Word.
- Вот где у меня проблемы. Моя цель - удалить пароль из файла 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>
А я вижу. Большое спасибо! Я попробую, когда я вернусь за стол – Rob