PURPOSE
The script should iterate through each file in a folder, convert to .txt and upload text to Azure database
PROBLEM
Everything works fine up until it hits a password protected file, I just want to skip these files. I am running this on hundreds of thousands of documents and the script will pause if it hits a password protected file until you either enter the password or click Cancel.
SCRIPT
Write-Output "Processing: $($file)"
Try {
$doc = $word.Documents.OpenNoRepairDialog($file)
}
Catch {
}
if ($doc) {
$fileName = [io.path]::GetFileNameWithoutExtension($file)
$fileName = $filename + ".txt"
$doc.SaveAs("$env:TEMP\$fileName", [ref]$saveFormat)
$doc.Close()
$4ID = $fileName.split('-')[-1].replace(' ', '').replace(".txt", "")
$text = Get-Content -raw "$env:TEMP\$fileName"
$text = $text.replace("'", "")
$query += "
('$text', $4ID),"
Remove-Item -Force "$env:TEMP\$fileName"
}
SOLUTION
For anyone having the same issue, the solution was to pass a non empty string to the open call like so:
$wd.Documents.Open($file, $false, $falsel, $false, "ttt")
rather than
$wd.Documents.Open($file, $false, $falsel, $false, "")