简体   繁体   English

从任务计划程序运行的Powershell脚本无法打开Word文档

[英]Powershell script run from task scheduler unable to open word document

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. 我正在尝试使用Powershell脚本从多个Word文件中获取单词计数,然后将其输出到CSV文件。 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. 从powershell提示符运行时,此方法可以按预期工作,而从cmd提示符直接或从perl脚本中调用时,此方法可以工作,但是当作为计划任务调用时,脚本无法工作。

This is not an ExecutionPolicy issue causing the script to not run at all. 不是 ExecutionPolicy问题,不会导致脚本完全不运行。 The script is running and produces some output, but when launched from task scheduler it is unable to open any word documents. 该脚本正在运行并产生一些输出,但是从任务计划程序启动时,它无法打开任何Word文档。

The relevant powershell code is below: 相关的PowerShell代码如下:

$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. 或在计划任务启动的perl脚本中使用类似命令运行时(我的正常使用情况),并且在两种情况下,都将任务设置为以最高特权运行时,powershell脚本无法正常工作。

The catch block is apparently never reached, as the print statement never makes it to the file, however the $doc is always null. 显然,从来没有到达catch块,因为print语句从不进入文件,但是$ doc始终为空。

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. 另外,当作为任务启动时,脚本将打开单词处理程序,并为1个线程使用100%cpu,这最终会削弱我的计算机。

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. 从任务运行脚本(以管理员用户身份)->脚本运行但无法访问Word文档,即使总是按$ word.Quit行也无法停止单词处理。

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. 编辑:使用@TheMadTechnician有关为新用户首次启动办公室的建议时,需要一些详细信息:在进一步检查中,查看任务管理器中的流程,除非单击“显示所有用户的流程”,否则我看不到流程一词。 ,但随后出现,但列出了该用户。 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... 尝试在脚本中设置$word.visible = $true时,从任务计划程序启动时实际上没有任何显示,因此,我既不知道如何验证它正在等待输入,又不知道如何为它提供输入作为使它消失的正确用户...

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" . 您也许可以按照问题“ 如何与桌面交互”从计划程序运行Windows 2008任务中命名的解决方案进行操作。

Summary: If you have 64-bit Windows: Create %SystemRoot%\\SysWOW64\\config\\systemprofile\\Desktop . 摘要:如果您具有64位Windows:创建%SystemRoot%\\SysWOW64\\config\\systemprofile\\Desktop If you have 32-bit Windows, create %SystemRoot%\\system32\\config\\systemprofile\\Desktop . 如果您使用32位Windows,请创建%SystemRoot%\\system32\\config\\systemprofile\\Desktop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell 脚本不会在任务计划程序中打开或传递参数时运行 - Powershell Script Will not Run upon Open or Pass Parameter from Task Scheduler VBScript 不会从从 Windows 任务计划程序运行的 Powershell 脚本中运行 - VBScript will not run from Powershell script that runs from Windows Task Scheduler 任务计划程序中的Powershell脚本无法按预期工作 - Powershell script from task scheduler unable to work as intended 从任务计划程序运行时,PowerShell脚本失败 - PowerShell script failing when run from the Task Scheduler 从任务计划程序运行的PowerShell脚本中的Msgbox无法正常工作 - Msgbox in PowerShell script run from task scheduler not working Powershell脚本运行任务计划程序将无法启动Photoshop - Powershell script run task scheduler will not start photoshop Powershell 脚本无法通过任务计划程序运行 - Powershell script does not run via Task Scheduler 从任务计划程序运行 Powershell 脚本 - Running a Powershell script from Task Scheduler 如何从Powershell脚本调用任务计划程序中的任务? - How to call a task in Task Scheduler from a Powershell script? 从任务计划程序运行时,PowerShell 脚本将文件上传到 SharePoint 不起作用 - PowerShell Script uploading file to SharePoint doesn't work when run from Task Scheduler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM