Я использовал код, предоставленный @inwenis через http://pastebin.com/UCqXUYHU
похоже, что он работает и удалит один или два пароля для файлов в папке, но будет либо потерпеть неудачу после 2 решенных листов Excel, либо, если пароль будет более 30 строк в списке паролей.
Может кто-нибудь указать мне, где ошибка может быть в автоматизации его для более чем 2 листов или большего списка паролей.
param(
$encrypted_path = "C:\PoShTest\Encrypted\",
$decrypted_Path = "C:\PoShTest\Decrypted\",
$processed_Path = "C:\PoShTest\Processed\",
$password_Path = "C:\PoShTest\Passwords\Passwords.txt"
)
$ErrorActionPreference = "SilentlyContinue"
# Get Current EXCEL Process ID's so they are not affected by the scripts cleanup
$currentExcelProcessIDs = (Get-Proces s excel).Id
$startTime = Get-Date
Clear-Host
$passwords = Get-Content -Path $password_Path
$encryptedFiles = Get-ChildItem $encrypted_path
[int] $count = $encryptedFiles.count - 1
$ExcelObj = New-Object -ComObject Excel.Application
$ExcelObj.Visible = $false
$encryptedFiles | % {
$encryptedFile = $_
Write-Host "Processing" $encryptedFile.name -ForegroundColor "DarkYellow"
Write-Host "Items remaining: " $count
if ($encryptedFile.Extension -like "*.xls*") {
$passwords | % {
$password = $_
# Attempt to open encryptedFile
$Workbook = $ExcelObj.Workbooks.Open($encryptedFile.fullname, 1, $false,5, $password)
$Workbook.Activate()
# if password is correct save decrypted encryptedFile to $decrypted_Path
if ($Workbook.Worksheets.count -ne 0 ) {
$Workbook.Password = $null
$savePath = Join-Path $decrypted_Path $encryptedFile.Name
Write-Host "Decrypted: " $encryptedFile.Name -f "DarkGreen"
$Workbook.SaveAs($savePath)
# Added to keep Excel process memory utilization in check
$ExcelObj.Workbooks.close()
# Move original encryptedFile to $processed_Path
Move-Item $encryptedFile.fullname -Destination $processed_Path -Force
}
else {
$ExcelObj.Workbooks.Close()
}
}
}
$count--
}
# Close Document and Application
$ExcelObj.Workbooks.close()
$ExcelObj.Application.Quit()
$endTime = Get-Date
Write-Host "Processing Complete!" -f "Green"
Write-Host "Time Started : " $startTime.ToShortTimeString()
Write-Host "Time Completed : " $endTime.ToShortTimeString()
Write-Host "Total Duration : "
$startTime - $endTime
# Remove any stale Excel processes created by this scripts execution
Get-Process excel `
| Where { $currentExcelProcessIDs -notcontains $_.id } `
| Stop-Process