从任务计划程序运行的 Powershell 脚本无法打开 Word 文档 [英] Powershell script run from task scheduler unable to open word document

查看:88
本文介绍了从任务计划程序运行的 Powershell 脚本无法打开 Word 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 powershell 脚本从多个单词文件中获取字数,然后将其输出到 csv 文件.这在从 powershell 提示符运行时按预期工作,在直接从 cmd 提示符或从 perl 脚本内部调用时工作,但是当作为计划任务调用时,脚本无法工作.

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.

不是导致脚本根本无法运行的执行策略问题.脚本正在运行并产生一些输出,但是当从任务调度程序启动时,它无法打开任何 Word 文档.

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.

相关的powershell代码如下:

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

(请注意,人们引用的用于从 word 文档中获取字数的替代方法实际上并未返回正确的计数,因为该方法在工作时会这样做.)

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

或者当使用来自计划任务启动的 perl 脚本的类似命令(我的正常用例)运行时,在这两种情况下,任务都设置为以最高权限运行,powershell 脚本无法正常工作.

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.

显然永远不会到达 catch 块,因为打印语句永远不会到达文件,但是 $doc 始终为空.

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

此外,当作为任务启动时,脚本会打开一个字进程,并为 1 个线程使用 100% 的 CPU,这最终会导致我的机器瘫痪.

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.

总结:

像人类一样运行脚本(无论有多少间接级别)-> 完美运行

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

从任务运行脚本(以管理员用户身份)-> 脚本运行但无法访问 word 文档,尽管总是点击 $word.Quit 行,也无法停止 word 进程.

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.

@TheMadTechnician 关于新用户首次启动办公室的建议需要一些细节:进一步检查,查看任务管理器中的进程,我看不到进程这个词,除非我点击显示进程来自所有用户",但随后他们出现,但将用户列为我.一个进程如何既被明确列为我,又被视为另一个用户?

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?

尝试在脚本中设置 $word.visible = $true 从任务调度程序启动时实际上并没有显示任何内容,所以我不知道如何验证它正在等待用于输入,或者如何以正确的用户身份为其提供输入以使其消失...

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...

推荐答案

您或许可以按照问题中指定的解决方案进行操作 如何使用与桌面交互"从调度程序运行 Windows 2008 任务.

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".

总结:如果您有 64 位 Windows:创建 %SystemRoot%\SysWOW64\config\systemprofile\Desktop.如果您使用的是 32 位 Windows,请创建 %SystemRoot%\system32\config\systemprofile\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.

这篇关于从任务计划程序运行的 Powershell 脚本无法打开 Word 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆