【问题标题】:Skip password protected powerpoint files with powershell使用 powershell 跳过受密码保护的 powerpoint 文件
【发布时间】:2020-12-13 07:24:56
【问题描述】:

我正在编写一个用于扫描文件以查找 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\3(?!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);

任何帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: powershell passwords powerpoint comobject


    【解决方案1】:

    由于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
    #>
    

    【讨论】:

    • 很好(你不需要尾随的::)-我不知道这是可能的-它是否记录在某个地方?
    • 感谢您的编辑,我发布此内容时已晚。这种方法只是我过去在另一个客户 MSOffice/Outlook/Exchange 开发/数据分类/发现项目中使用 WIndows FSRM 实施时偶然发现的东西。由于该方法解决了我过去的问题,使我能够完成该项目,因此我再也没有回去进一步研究它。然而,在普通文档中,我没有在任何地方看到它被 MS 记录,因为我目前正在从事类似的 (Azure RMS/AIP/MIP) 项目。
    • 是的!谢谢你。这种方法有效。我正在使用“$pptfile::password”和“{$pptfile::password}”,但它们不起作用。 "$pptFile::{password}::" 成功了。
    • 不用担心。很高兴它有帮助。出于礼貌,请记住标记您已接受的答案,以便其他可能发现自己处于同一用例中的人受益。
    • 没问题。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2015-11-15
    • 2011-02-06
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多