将Powershell脚本作为计划任务运行 [英] Running Powershell script as scheduled task

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

问题描述

我正在使用下面的powershell脚本打开多个Word文档,更新其中的表单字段,然后关闭它们.首先,我会注意到,如果此脚本是从命令行运行的,或者通过右键单击文件并选择Run with Powershell基本上以任何方式手动进行操作,它将很好地执行.但是,如果我尝试将其作为计划任务运行,则会发生以下两种情况之一.

I am using the powershell script below to open several word documents, update the form fields in them, and then close them. First, I'll note that if this script is run from the command line or by right clicking the file and selecting Run with Powershell basically doing it manually in any way, it will execute just fine. However, if I try to run this as a scheduled task, one of two things happen.

我为此尝试了几种不同形式的语法,但是都产生相同的两个结果之一. A)脚本说它启动并在大约3秒钟内完成而不完成任何操作,或者B)脚本启动,我可以看到Word在任务管理器中打开,但是没有命令窗口显示进度,就像正常情况下一样,而Word仅大约2分钟后挂断电话.确实,使我进入选项B的唯一语法是将powershell作为操作,将.\ Script1.ps1作为参数,并将文件夹作为开始位置.我尝试了这种语法的一些细微变化,例如使用了powershell的完整路径,等等.当我在挂起任务管理器中的Word时,当它挂起时,计划任务表示已成功完成.有人有想法么?脚本如下:

I have tried several different forms of syntax for this, but all produce one of the same two results. Either A) The script says it started and finishes in about 3 seconds without doing anything, or B) The script starts, I can see Word open in task manager, but there is no command window showing the progress like there is normally and Word just gets hung up after about 2 minutes. Really the only syntax that got me to option B there is putting powershell as the action, .\Script1.ps1 as the argument, and the folder as the start in location. I tried a few slight variations of this syntax, such as using the full path of powershell, etc. When I end Word in task manager when it hangs, the scheduled task says that it completed successfully. Anyone have any ideas? The script is as follows:

    $filearray = 1..12
    $filename="E:\Form0801"
    $word=new-object -com Word.Application
    $word.Visible=$False            #Making this true 
    #does not show word if run from scheduled task
    Write-Host "DO NOT CLOSE THIS WINDOW!"

    try
    {
    foreach ($i in $filearray)
    {
        if($i -lt 10){$filename="E:\Form080" + $i + ".dot"}
        else{$filename="E:\Form08" + $i + ".dot"}
        $doc=$word.Documents.Open($filename)
        $word.ActiveDocument.Fields.Update()
        $doc.SaveAs([REF]$filename)
        $doc.Close()
        Write-Host "File " $filename " has been updated successfully"
    }

    }
    catch [System.Management.Automation.ActionPreferenceStopException] 
    {
        "A problem occurred while opening one of the files."
        $error[0]
    }

$word.Quit()
# End Word Application

推荐答案

弄清楚了.我什至没有想到要在32和64版本的Powershell中设置执行策略.手动运行它时,它使用的是一个,而通过预定任务运行时,则使用了另一个.一旦确定两者都设置好,一切就很好了.谢谢大家的答复.

Figured it out. I didn't even think to set the execution policy in both the 32 and 64 versions of powershell. When running it manually, it was using one, and running it through scheduled tasks was using the other. Once I made sure it was set it both, everything is good to go. Thank you all for your responses.

这篇关于将Powershell脚本作为计划任务运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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