2
votes

I am trying to use a powershell script to get the wordcount from a number of word files and then output that to a csv file. This works as expected when run from the powershell prompt, and works when called from the cmd prompt directly or from inside a perl script, however the script fails to work when called as a scheduled task.

This is not an ExecutionPolicy issue causing the script to not run at all. The script is running and produces some output, but when launched from task scheduler it is unable to open any word documents.

The relevant powershell code is below:

$folderpath = "C:\some\where\*"
$fileTypes = "*.docx"

$word = New-Object -ComObject word.application
$word.visible = $false
Get-ChildItem -path $folderpath -recurse -include $fileTypes |
foreach-object `
{
  $path =  ($_.fullname).substring(0,($_.FullName).lastindexOf("."))

  try {
    $doc = $word.documents.open($_.fullname, $confirmConversion, $readOnly, $addToRecent, $passwordDocument)
  } catch {
    "FROM CATCH UNABLE TO OPEN $($_.fullname)" >> $wordCountFile
  }
  if($doc) {
    $wordCount = $doc.ComputeStatistics("wdStatisticWords")
    "$($_.name), $wordCount"  >> $wordCountFile
    $doc.close([ref]$false)
  } else {
    "UNABLE TO OPEN $($_.fullname)" >> $wordCountFile
  }

} #end Foreach-Object
$word.Quit()

(Note that alternative methods people cite for getting the word count from word documents do not actually return the correct count as this method does when it works.)

When run as a scheduled task (set to run with highest privileges and as an administrator user) using:

cmd /c start PowerShell.exe -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\path\to\script\CountsInWords.ps1"

or when run with a similar command from a perl script launched by a scheduled task (my normal use case) and in both cases the task is set to run with highest privileges the powershell script does not work correctly.

The catch block is apparently never reached, as the print statement never makes it to the file, however the $doc is always null.

Additionally, when started as a task the script leaves a word process open and using a 100% cpu for 1 thread which will eventually cripple my machine.

To summarise:

run script as human (no matter how many levels of indirection) -> works perfectly

run script from task (as administrator user) -> script runs but cannot access word documents, also unable to stop word process despite always hitting the $word.Quit line.

EDIT: With @TheMadTechnician's advice about office first time startup for a new user requiring some details: On further inspection, looking at the processes in task manager, I don't see the word processes unless I click on "show processes from all users", but then they show up, but have the user listed as me. How can a process be both explicitly listed as me, but count as another user?

Attempting to set $word.visible = $true in the script didn't actually make anything appear when launched from task scheduler, so I don't know either how to verify that it is waiting for input, or how to give it that input as the correct user to make it go away...

1
Is the account you are running from setup in Office, or is Word opening to the prompt of "Enter user name and initials" as any office application will on first launch (once per suite/user)? If it's at that prompt then it won't do anything until the dialog box is closed.TheMadTechnician
Interesting point. As far as I know the service is set up to run as my user, the "when running the task, use the following user account" field specifies my user, which is also listed as the task author. I'll see if I can modify the script to not open word as invisible and see if something useful shows up there.user2711915
Try the solution documented here: stackoverflow.com/questions/1674836/…Bill_Stewart
Oh come on? That actually worked? Please submit an answer so I can credit you fully.user2711915
Added answer as requested.Bill_Stewart

1 Answers

3
votes

You may be able to follow the solution named at the question How to run a Windows 2008 task from the scheduler with "interact with desktop".

Summary: If you have 64-bit Windows: Create %SystemRoot%\SysWOW64\config\systemprofile\Desktop. If you have 32-bit Windows, create %SystemRoot%\system32\config\systemprofile\Desktop.