excel - 在powershell中从excel文件中删除密码
问题描述
我一直在使用@inwenis 通过http://pastebin.com/UCqXUYHU提供的代码
它似乎可以工作,并将删除文件夹中文件的一个或 2 个密码,但在 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
解决方案
推荐阅读
- azure-data-factory - ADF Oracle 动态范围分区选项
- python-3.x - 如何使用 Tensorflow from_generator 将多个序列和标签的生成器转换为数据集?
- matlab - MATLAB 中用于距离-方位角映射的 2D-MUSIC 算法
- javascript - 通过 _id 查找总是比通过不同的唯一字段查找更快吗?(mongodb)
- java - 将 REST 响应映射到 Mono 的正确方法
使用 WebClient/Spring Boot 2 - java - @InjectMocks 没有正确注入依赖项
- r - 将 ConditionalPanel 与 checkboxGroupInput 一起使用
- layout - Ngx 管理模板侧边栏和内容布局
- javascript - 验证数组 Json Javascript
- flutter - 如何在 Flutter 的输入文本字段中动态渲染 TeX 方程?