Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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()

share|improve this question
    
I don't see any screenshots. –  dangph Jan 6 at 1:47
    
Something I'm not clear about: Can there be more than one instance of your script running at a time? If that's the case, then you will be doing multiple concurrent operations in Word, but Word is not designed for that. It can only do one thing at a time. Word is a single-user client application. It wasn't designed to be used as a server application that can handle multiple simultaneous requests. I recall that there is a Knowledge Base article from MS about this. –  dangph Jan 6 at 1:59
    
What account runs the task? Might try setting visibility to false so it starts the process but does try showing the window. –  Shawn Melton Jan 6 at 10:33
    
Add the administrator to Logon as a batch job per GPO. This may help. –  user3122801 Jan 9 at 7:19
    
The limitation on using Office COM objects in non desktop environments has been explained before: superuser.com/questions/730474/… –  MFT Apr 16 at 5:36

1 Answer 1

The problem is likely to be specific to MS Word, which isn't designed or supported to run in a non-interactive session. Best solution is to find some other way to do your PDF conversion.

See

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.