I have a powershell script that converts .doc to .pdf files. When I execute it manually, it works perfectly on my windows server 2012.
When I execute it as a scheduled task, opens an instance of Word, but cant close it, and the task don't end correctly. The worst part is that this process utilizes 10% of the procesor and when the task runs again, another one opens on top of it, and this keeps happening using 100% of the cpu.
Both times, manually and from scheduled task its runs as administrator... and the task is well created, if I modify the script and dont open a Word and just for example create a .txt file, works fine. so the problems its there. "opening word from task scheduler"
Here's the scripts, and also screenshots. Any help will be appreciated!
$origen = 'C:\Test'
$destino = 'C:\Test'
$word_app = New-Object -ComObject Word.Application
echo "Buscando cambios en las carpetas de origen..."
Get-ChildItem -Path $origen -Filter *.doc? | ForEach-Object {
if (-Not (Test-Path "$destino\$($_.BaseName).pdf")) {
$document = $word_app.Documents.Open($_.FullName)
$pdf_filename = "$destino\$($_.BaseName).pdf"
echo "$($_.FullName) convertido a $pdf_filename!"
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()
}
}
$word_app.Quit()