0

We're trying to use Jenkins to convert a set of "Working Documents" into "Release Documents" when it builds out project. This involved taking the .docx files and saving them as .pdf files, which we accomplish with the following Powershell script:

$documents_path = "E:\Documentation\Working Documents\"

$word_app = New-Object -ComObject Word.Application

echo $word_app

# This filter will find .doc as well as .docx documents
$files = Get-ChildItem -Path $documents_path -Filter *.doc? 

ForEach ($file in $files) {
    echo "Converting Document to PDF: $($file.FullName)"

    $document = $word_app.Documents.Open($file.FullName)
    $pdf_filename = "$($file.DirectoryName)\..\Release Documents\$($file.BaseName).pdf"
    $document.SaveAs([ref] $pdf_filename, [ref] 17)
    $document.Close()
}

$word_app.Quit()

Now, that script works 100% the way we expect when I log in to the Jenkins PC and run it myself in Powershell. However, when Jenkins tries running it we get You cannot call a method on a null-valued expression at $document.SaveAs and $document.Close.

I assume this is because the user Jenkins runs as, SYSTEM does not have permission to access the .docx files, or can't find the Word installation, or something of that nature, but I can't think of how I should try to debug it further than this. Any tips are very much appreciated!

DTI-Matt
  • 2,065
  • 9
  • 35
  • 60
  • You've already identified two possible causes (permissions on the files, inability to launch Word), so start by making changes to address **one at a time** and see if it resolves the problem. Try running Jenkins as a user that *does* have permission to execute Word. If that doesn't work, start looking at permissions on files and folders. – alroc Oct 10 '13 at 14:34
  • I will start the opposite way, as I would assume the null would have come from $word_app had I not had permission to launch Word. – DTI-Matt Oct 10 '13 at 14:43
  • That's what my first suggestion was. Run the Jenkins service as a user that does have permission to execute applications. [SYSTEM can't run desktop apps](http://stackoverflow.com/questions/5001065/how-can-i-run-a-application-under-system). And Jenkins probably shouldn't be running as SYSTEM in the first place. – alroc Oct 10 '13 at 14:52
  • That's the way it was installed by default, and I can't find much in terms of changing the user the service runs as. – DTI-Matt Oct 10 '13 at 15:00
  • You can do it via the Services MMC snap-in. – alroc Oct 10 '13 at 15:25
  • @alroc I've changed Jenkins to run, temporarily /proof of concept as my user, the same user that is able to run the PowerShell script manually. I've also ensured my user has full control over both the source and destination directories and still no dice! Any further tips? – DTI-Matt Oct 10 '13 at 20:06

1 Answers1

3

I had the same problem and found a simple workaround.

Create an empty directory "Desktop" in

C:\Windows\SysWOW64\config\systemprofile\
  • This worked for me. Changing who the service runs as did not work, unless the service was running as the currently logged-in user, which kind of defeats the point of running as a service. – Darryl Feb 21 '14 at 00:08