Powershell scripts works manually but not as a scheduled task

4

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

Andrew

Posted 2014-01-03T21:02:53.670

Reputation: 41

I don't see any screenshots. – dangph – 2014-01-06T01:47:47.497

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 – 2014-01-06T01:59:31.137

What account runs the task? Might try setting visibility to false so it starts the process but does try showing the window. – None – 2014-01-06T10:33:48.257

Add the administrator to Logon as a batch job per GPO. This may help. – Berndinox – 2014-01-09T07:19:17.323

The limitation on using Office COM objects in non desktop environments has been explained before: http://superuser.com/questions/730474/cant-create-a-com-object-in-a-powershell-script-running-as-a-scheduled-task

– MFT – 2014-04-16T05:36:32.527

Answers

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

Rory

Posted 2014-01-03T21:02:53.670

Reputation: 1 473