1

I'm trying to run a PowerShell script through the Jenkins plugin that will convert Word documents to text but getting Null type errors. The slave is a Win 2008 box, Jenkins is running as a service on it and the service is running as Administrator. I've tried:

  • Verifying that the commands work on the remote box by running them locally.
  • Running the PowerShell script using Windows batch (same error).
  • Running the commands through Jenkins plugin.

Script ($Doc is being set to 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()
}

Confirmed that a) there was a file and b) that I got a Word object. Again, all this works fine on the remote box.

$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                : 
.
.
.

The error occurs at the close because $Doc never got set. When I tried to print the $Doc during execution, nothing was displayed.

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

Updated:

Changed script as suggested by Andreas M.:

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

Same error but oddly enough, no errors reported from the Word.Doc.Open call.

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
stackbacker
  • 329
  • 2
  • 11

1 Answers1

1

Try the following. First of all check if Administrator has the rights to open files under the given location. Extend the script with error output the check why $Word.Documents.Open($File.FullName) returns $null.

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

Maybe you can retrieve additional information why Open fails.

UPDATE: You maybe have to change your Com/Dcom settings -> check the answer of this link.

Community
  • 1
  • 1
Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • Updated initial question with error output. I've logged into the remote box with the same username/password that Jenkins uses and am able to run the script without issue. – stackbacker Feb 12 '17 at 20:46
  • Thank you very much for the link. I feel 99% confident that I would never have looked in that direction on my own. While it wasn't the DCOM issue, it was adding Desktop to the SysWow directory (mentioned in that link) that fixed it. Again - thank you. – stackbacker Feb 13 '17 at 15:41