2017-02-11 43 views
1

我試圖通過Jenkins插件運行PowerShell腳本,該插件將Word文檔轉換爲文本,但獲取空類型錯誤。奴隸是一個Win 2008盒子,Jenkins作爲服務運行,服務以管理員身份運行。我試過了:Jenkins PowerShell插件 - 返回空

  • 通過本地運行驗證命令是否在遠程盒子上工作。
  • 使用Windows批處理(相同錯誤)運行PowerShell腳本。
  • 通過Jenkins插件運行命令。

腳本($Doc被設置爲NULL):

$Files = Get-ChildItem 'PTX*.docx' 
$Files 
$Word = New-Object -ComObject Word.Application 
$Word 

foreach ($File in $Files) { 
    # open document in Word 
    $File.FullName 
    $Doc = $Word.Documents.Open($File.FullName) 
    $Doc 
    Start-Sleep -s 10 
    # swap out RTF for TXT in the filename 
    #$Name = ($Doc.FullName).Replace("docx", "txt") 
    #$Doc.SaveAs([ref] $Name, [ref] 2) 

    $Doc.Close() 
} 

確認了)有一個文件和b),我得到了一個Word對象。再一次,所有這些在遠程盒子上都能正常工作。

$Word

Application     : Microsoft.Office.Interop.Word.ApplicationClass 
Creator      : 1297307460 
Parent      : Microsoft.Office.Interop.Word.ApplicationClass 
Name       : Microsoft Word 
Documents      : System.__ComObject 
Windows      : System.__ComObject 
ActiveDocument    : 
. 
. 
.

錯誤發生在靠近因爲$Doc從來沒有得到設定。當我在執行過程中試圖打印$Doc時,沒有顯示任何內容。

C:\jenkins\workspace\eggplant-Test\DVA.docx 
You cannot call a method on a null-valued expression. 
At C:\Users\PENDAN~1.MDZ\AppData\Local\Temp\hudson1097244472905940013.ps1:19 char:12 
+  $Doc.Close <<<<() 
    + CategoryInfo   : InvalidOperation: (close:String) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull

更新時間:由Andreas中號

更改腳本的建議:

Foreach ($File in $Files) { 
    # open document in Word 
    echo File: $File.fullname 
    $Error.Clear() # Clear all other errors 
    $Doc = $Word.Documents.Open($File.FullName) 
    echo "Error count:" $Error.Count # Dump number of errors 
    $Error # Dump errors  
     echo "Doc:" $Doc 

同樣的錯誤,但奇怪的是,無論從Word.Doc.Open調用報告的錯誤。

File: 
C:\jenkins\workspace\eggplant-Test\DVA.docx 
Error count: 
0 
Doc: 
You cannot call a method on a null-valued expression. 
At C:\Users\PENDAN~1.MDZ\AppData\Local\Temp\hudson3349169014447704754.ps1:23 ch 
ar:12 
+  $Doc.close <<<<() 
    + CategoryInfo   : InvalidOperation: (close:String) [], RuntimeExce 
    ption 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

回答

1

請嘗試以下操作。首先檢查Administrator是否有權在給定的位置下打開文件。用錯誤輸出的腳本擴展腳本爲什麼$Word.Documents.Open($File.FullName)返回$null

$Error.Clear() # Clear all other errors 
$Doc = $Word.Documents.Open($File.FullName) 
$Error.Count # Dump number of errors 
$Error # Dump errors 

也許你可以檢索到更多的信息爲什麼Open失敗。

更新:您可能必須更改您的Com/Dcom設置 - >檢查此link的答案。

+0

已更新的初始問題與錯誤輸出。我使用Jenkins使用的相同用戶名/密碼登錄到遠程盒子,並且能夠毫無問題地運行腳本。 – stackbacker

+0

非常感謝您的鏈接。我感到99%有信心,我永遠不會在自己的方向看這個方向。雖然它不是DCOM問題,但是它將Desktop添加到修復它的SysWow目錄(在該鏈接中提到)。再次謝謝你。 – stackbacker

+0

很高興在這裏。別客氣。 – Moerwald