PoshCode Archive  Artifact [2dba5425ce]

Artifact 2dba5425ceedaa040534e8c8914a28bfb9cd84bdb2a3e2da8a4bfea3f5b2b1e5:

  • File Office-Pass-SaveAs.ps1 — part of check-in [454c31fb43] at 2018-06-10 13:49:42 on branch trunk — Use known password cache to open and save unprotected copies of documents. – work in progress (user: Steele Stenger size: 3969)

# encoding: ascii
# api: powershell
# title: Office Pass SaveAs
# description: Use known password cache to open and save unprotected copies of documents.   – work in progress
# version: 0.1
# author: Steele Stenger
# license: CC0
# x-poshcode-id: 5073
# x-archived: 2014-04-13T13:00:28
# x-published: 2014-04-11T21:15:00
#
#
$ErrorActionPreference = "SilentlyContinue"

CLS

# Paths
$encrypted_path = "C:\Password_Recovery\Encrypted"
$decrypted_Path = "c:\Password_Recovery\Decrypted\"
$original_Path = "c:\Password_Recovery\Originals\"
$password_Path = "c:\Password_Recovery\Passwords\Passwords.txt"

# Load Password Cache
$arrPasswords = Get-Content -Path $password_Path

# Load File List
$arrFiles = Get-ChildItem $encrypted_path 

# Create counter to display progress
[int] $count = ($arrfiles.count -1)

# Loop through each file
$arrFiles| % {
    $file  = get-item -path $_.fullname 
    # Display current file
    write-host "Processing" $file.name -f "DarkYellow"
    write-host "Items remaining: " $count `n

    # Word docx
    if ($file.Extension -eq ".docx") {

    # Loop through password cache
        $arrPasswords | % {
            $WordObj = $null

            # New Word Object
            $WordObj = New-Object -ComObject Word.Application
            $WordObj.Visible = $false
            $WordObj.Options.WarnBeforeSavingPrintingSendingMarkup = $false
            $passwd = $_

            # Attempt to open file
            $WordDoc = $WordObj.Documents.Open($file.fullname, $null, $false, $null, $passwd, $passwd)
            $WordDoc.Activate()
            $content = $null
            $content = $WordDoc.content

            # if password is correct - Save new file without password to $decrypted_Path
            if ($content -ne $null) {
                write-host "opened " -f "Yellow"
                $WordDoc.Password=$null
                $savePath = $decrypted_Path+$file.Name
                write-host "Decrypted: " $file.Name -f "DarkGreen"
                $WordDoc.SaveAs([ref]$savePath)
            # Close document and Application
                $WordDoc.Close()
                $WordObj.Application.Quit()

            # Move original file to $original_Path
                move-item $file.fullname -Destination $original_Path -Force
                }
            else {
            # Close document and Application
                $WordDoc.Close()
                $WordObj.Application.Quit()
            }
        }
    }
    # Excel xlsx
    elseif ($file.Extension -eq ".xlsx") {

    # Loop through password cache
        $arrPasswords | % {
            $ExcelObj = $null
        
            # New Excel Object
            $ExcelObj = New-Object -ComObject Excel.Application
            $ExcelObj.Visible = $false

            $passwd = $_
            # Attempt to open file
            $Workbook = $ExcelObj.Workbooks.Open($file.fullname,1,$false,5,$passwd)
            $Workbook.Activate()

            # if password is correct - Save new file without password to $decrypted_Path
                if ($Workbook.Worksheets.count -ne 0) {
                    $Workbook.Password=$null
                    $savePath = $decrypted_Path+$file.Name
                    write-host "Decrypted: " $file.Name -f "DarkGreen"
                    $Workbook.SaveAs($savePath)
            # Close document and Application
                    $ExcelObj.Workbooks.close()
                    $ExcelObj.Application.Quit()

            # Move origanl file to $original_Path
                    move-item $file.fullname -Destination $original_Path -Force
                }
                else {
            # Close document and Application
                    $ExcelObj.Close()
                    $ExcelObj.Application.Quit()
                }
        }

    }
$count--
# Next File
}

Write-host "`n Complete" -f "Green"