使用 powershell 跳过密码保护的 powerpoint 文件

Skip password protected powerpoint files with powershell

我正在编写一个脚本来扫描文件中的 SSN。我已经完成了大部分工作,但是这部分让我很伤心,我找不到任何有用的文档。

我 运行 遇到了跳过受密码保护的 PowerPoint 文件的问题。

#Scan for SSN in pptx

$findtext = "\b(?!0{3}|6{3})([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d(?!0000)\d{4}\b"
$pptfiles = Get-Childitem -Path $searchpath -Include *.ppt, *.pptx -Recurse
# Create instance of the PowerPoint.Application COM object
$ppt = New-Object -ComObject PowerPoint.Application
# walk through the collection of slides
foreach ($pptfile in $pptfiles) {

          $presentation = $ppt.Presentations.open($pptfile, [Microsoft.Office.Core.MsoTriState]::msoTrue,
                [Microsoft.Office.Core.MsoTriState]::msoFalse, [Microsoft.Office.Core.MsoTriState]::msoFalse)

  $slidecount = $presentation.Slides.Count
      $slideCounter = 1
      WHILE ($slideCounter -le $slideCount) {      
            $slide = $presentation.slides.item($slidecounter)
            $title = $slide.Shapes[1].TextFrame.TextRange.Text
            $body = $slide.Shapes[2].TextFrame.TextRange.Text
            $arrcontents = @($title, $body)
            ForEach ($line in $arrcontents) {
                  $pptline = $line.trim()
                  If ($pptline -match $findtext) {
                        $Violation = [PSCustomObject]@{
                              Path       = $pptfile | Select-Object | Select-object -ExpandProperty fullname
                              Line       = $pptline
                              LineNumber = "Slide $slideCounter"
                        }
                        $Violation | Select-Object Path, Line, LineNumber | export-csv $outputpath$PIIFile -append -NoTypeInformation
                  }
            }
            $slideCounter++
      }
      # prevent the Save Presentation prompt from displaying
      $presentation.saved = $true
      # close presentation
      $presentation.close()
} #end for
# release memory
$ppt.quit()
# release the memory immediately
$ppt = $null
$file = $null
Stop-Process -name POWERPNT

我无法在不打开文件的情况下自动检查是否有密码,这很矛盾,所以我的计划是在打开文件时使用 try-catch 语句(从当前代码中删除),并通过输入密码。问题是 PowerPoint 的参数与 Word 或 Excel 的参数不同,因此在第 5 个参数中输入假密码不起作用。这个方法(下),好像也行不通。

Presentation presentation = ppApp.Presentations.Open($"{presentationFile}::{password1}::", MsoTriState.msoFalse, MsoTriState.msoFalse, WithWindow: MsoTriState.msoFalse);

如有任何帮助,我们将不胜感激。提前致谢。

由于 Presentations.Open() 没有密码选项,您可以使用现有的解决方法来执行此操作。

Presentations.Open("c:\temp\open.pptx::password::")

传递一个错误的密码会导致一个可捕获的错误,因此...我创建了两个新的空白 pptx 文件并用密码保护一个而不是另一个,然后这样做:

输入错误密码

Clear-Host
($filenames = (Get-ChildItem -Path 'D:\Temp' -FIlter '*protected*.ppt*').FullName)

$application = New-Object -ComObject powerpoint.application
$application.visible = "msoTrue"

$filenames | 
ForEach-Object {
    Try
    {
        ($presentation = $application.Presentations.open("$PSitem::{password1}::"))
        Write-Verbose -Message "$PSitem access successful" -Verbose
        $presentation.Close()
    }
    Catch {Write-Warning -Message 'Open failed. Either the file is password protected, corrupt or some other issue'}
    Finally 
    {
        Write-Host "Closing $PSItem" -ForegroundColor Yellow
    }
}

$application.Quit()
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

# Results
<#
D:\Temp\PresentationNotProtected.pptx
D:\Temp\PresentationProtected.pptx


Application                      : Microsoft.Office.Interop.PowerPoint.ApplicationClass
...
FullName                         : D:\Temp\PresentationNotProtected.pptx
Name                             : PresentationNotProtected.pptx
Path                             : D:\Temp
Saved                            : 0
...
PasswordEncryptionProvider       : 
PasswordEncryptionAlgorithm      : 
PasswordEncryptionKeyLength      : 0
PasswordEncryptionFileProperties : True
Password                         : ********
WritePassword                    : ********
Permission                       : 
...

VERBOSE: D:\Temp\PresentationNotProtected.pptx access successful
Closing D:\Temp\PresentationNotProtected.pptx
WARNING: Open failed. Either the file is password protected, corrupt or some other issue
Closing D:\Temp\PresentationProtected.pptx
#>

输入正确的密码

($presentation = $application.Presentations.open("$PSitem::password::"))
<#
D:\Temp\PresentationNotProtected.pptx
D:\Temp\PresentationProtected.pptx


Application                      : Microsoft.Office.Interop.PowerPoint.ApplicationClass
...
FullName                         : D:\Temp\PresentationNotProtected.pptx
Name                             : PresentationNotProtected.pptx
Path                             : D:\Temp
...
PasswordEncryptionProvider       : 
PasswordEncryptionAlgorithm      : 
PasswordEncryptionKeyLength      : 0
PasswordEncryptionFileProperties : True
Password                         : ********
WritePassword                    : ********
...

VERBOSE: D:\Temp\PresentationNotProtected.pptx access successful
Closing D:\Temp\PresentationNotProtected.pptx

Application                      : Microsoft.Office.Interop.PowerPoint.ApplicationClass
...
FullName                         : D:\Temp\PresentationProtected.pptx
Name                             : PresentationProtected.pptx
Path                             : D:\Temp
...
PasswordEncryptionProvider       : 
PasswordEncryptionAlgorithm      : 
PasswordEncryptionKeyLength      : 0
PasswordEncryptionFileProperties : True
Password                         : ********
WritePassword                    : ********
Permission                       : 
...

VERBOSE: D:\Temp\PresentationProtected.pptx access successful
Closing D:\Temp\PresentationProtected.pptx
#>