2013-01-31 187 views
0

我正在使用下面的powershell腳本來打開幾個Word文檔,更新它們中的表單域,然後關閉它們。首先,我會注意到,如果這個腳本是從命令行運行的,或者右鍵單擊該文件並選擇Run with Powershell(基本上是以任何方式手動執行),它都會執行得很好。但是,如果我嘗試將此作爲計劃任務運行,則會發生以下兩種情況之一。按計劃任務運行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

我的第一個猜測是,這是一些特權問題。您使用哪個用戶來運行計劃任務?該用戶是否有權讀取和寫入文件? – Gisli

+2

用「hello」|嘗試調度一些test.ps1 out-file。\ test.txt並簽出[此腳本專家帖子](http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/12/use-scheduled-tasks-to-run- powershell-commands-on-windows.aspx)和[此服務器故障線程](http://serverfault.com/questions/101671/scheduled-tasks-w-gui-issue) – noam

回答

3

想通了。我甚至沒有想過要在32和64版本的PowerShell中設置執行策略。當手動運行它時,它正在使用一個,並通過計劃任務運行它正在使用另一個。一旦我確定它確定了它,一切都很好。謝謝大家的迴應。

0

請評論每一行寫入主機,並嘗試運行此計劃任務的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 
2

保持你的腳本,因爲它是,並嘗試從PowerShell命令行下面。

​​

這將創建一個weekely任務,最終線路將立即執行計劃任務,所以你可以測試它是否工作。如果確實如此,那麼您可以修改$ 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 = "schtasks /CREATE /XML 'C:\temp\Word Doc.xml' /TN 'Word Doc'" 

在有需要時反映正確的文件名,請確保您更改值秒。

+0

如果我想要如何重寫計劃行每天兩次,午夜一次,早上五點一次? –

+0

我會發布另一個答案,因爲我沒有足夠的空間。 –

+0

我已經更新了原來的答案 –

1

你確定你的腳本不僅僅因爲你的shell內存不足而失敗嗎? 你可以用它增加:具有這樣的問題腳本

Set-item wsman:localhost\Shell\MaxMemoryPerShellMB 512 

我們有問題,如你所以它總是值得一試。

+0

試過了,沒有運氣。如果這是問題,手動運行ps時腳本是否可以工作? –

+0

真是太長鏡頭了。 –

0

聽起來好像你想在運行腳本時看到腳本的進度,但如果這不重要,你可以嘗試創建一個.vbs腳本並運行它。

command = "powershell.exe -command C:\test\C:\temp\Script1.ps1" 
set shell = CreateObject("WScript.Shell") 
shell.Run command,0 
Set WinScriptHost = Nothing 
相關問題