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