我正在使用下面的powershell
脚本打开多个word文档,更新其中的表单字段,然后关闭它们。首先,我要注意,如果从命令行运行此脚本,或者右键单击文件并选择Run with Powershell
基本上以任何方式手动执行,它将执行得很好。但是,如果我尝试将其作为计划任务运行,则会发生以下两种情况之一。
我为此尝试了几种不同形式的语法,但都产生了相同的两种结果之一。 A)脚本说它开始并在大约3秒内完成而没有做任何事情,或者B)脚本启动,我可以在任务管理器中看到Word打开,但是没有命令窗口显示正常情况下的进度和Word只是大约2分钟后挂断电话。真的是让我选择B的唯一语法是将powershell
作为操作,。\ Script1.ps1作为参数,将文件夹作为起始位置。我尝试了这种语法的一些细微变化,例如使用powershell
等的完整路径。当我在任务管理器中挂起Word时,计划任务表明它已成功完成。有人有主意吗?脚本如下:
$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
答案 0 :(得分:3)
想出来。我甚至没想过要在powershell的32和64版本中设置执行策略。手动运行时,它正在使用一个,并通过计划任务运行它正在使用另一个。一旦我确定它都设置了它,一切都很好。谢谢大家的回复。
答案 1 :(得分:2)
保持脚本不变,并从Powershell命令行中尝试以下操作。
$Schedule = "schtasks /CREATE /TN 'Word Doc' /SC WEEKLY /RL HIGHEST /TR powershell 'C:\temp\Script1.ps1' /F"
$Start = "schtasks /RUN /TN 'Word Doc'"
$Schedule = [ScriptBlock]::Create($Schedule)
$Start = [ScriptBlock]::Create($Start)
Invoke-Command -ScriptBlock $Schedule
Invoke-Command -ScriptBlock $Start
这将创建一个周末任务,最后一行将立即执行计划任务,以便您可以测试它是否有效。如果是,那么您可以修改$ Schedule以匹配您需要的开始时间。
我找不到使用schtasks命令行中两个单独日期运行任务的方法,但我找到了一个解决方法如下:
1)创建以下XML,其中包含时间定义:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers>
<CalendarTrigger>
<StartBoundary>2013-02-01T00:00:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
<CalendarTrigger>
<StartBoundary>2013-02-01T05:30:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Actions Context="Author">
<Exec>
<Command>powershell</Command>
<Arguments>C:\temp\Script1.ps1</Arguments>
</Exec>
</Actions>
</Task>
2)按如下方式设置$ Schedule变量:
$Schedule = "schtasks /CREATE /XML 'C:\temp\Word Doc.xml' /TN 'Word Doc'"
确保更改所需的值以反映正确的文件名。
答案 2 :(得分:1)
你确定你的脚本不仅因为你的shell内存耗尽而失败吗? 您可以使用以下命令增加它:
Set-item wsman:localhost\Shell\MaxMemoryPerShellMB 512
我们遇到过像你这样的问题的脚本问题所以总是值得一试。
答案 3 :(得分:0)
请使用Write-Host注释每一行,并尝试使用schedule task运行此powershell。使用计划任务管理器时,写入主机错误。
$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
答案 4 :(得分:0)
听起来您希望在脚本运行时看到脚本的进度,但如果这不重要,您可以尝试创建.vbs脚本并运行它。
command = "powershell.exe -command C:\test\C:\temp\Script1.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0
Set WinScriptHost = Nothing