【问题标题】:Jenkins PowerShell Plugin - Returning NullJenkins PowerShell 插件 - 返回 Null
【发布时间】:2017-02-11 23:54:04
【问题描述】:

我正在尝试通过 Jenkins 插件运行 PowerShell 脚本,它将 Word 文档转换为文本,但出现 Null 类型错误。从机是一个 Win 2008 机器,Jenkins 作为服务运行在其上,服务以管理员身份运行。我试过了:

  • 通过在本地运行命令来验证这些命令在远程机器上是否有效。
  • 使用 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()
}

确认 a) 有一个文件 b) 我得到了一个 Word 对象。同样,所有这些都可以在远程盒子上正常工作。

$Word:

应用程序:Microsoft.Office.Interop.Word.ApplicationClass
创建者:1297307460
父级:Microsoft.Office.Interop.Word.ApplicationClass
名称:微软Word
文件:System.__ComObject
Windows:系统.__ComObject
活动文档:
.
.
.

错误发生在关闭时,因为$Doc 从未被设置。当我在执行期间尝试打印$Doc 时,没有显示任何内容。

C:\jenkins\workspace\eggplant-Test\DVA.docx
您不能在空值表达式上调用方法。
在 C:\Users\PENDAN~1.MDZ\AppData\Local\Temp\hudson1097244472905940013.ps1:19 char:12
+ $Doc.关闭 

更新:

按照 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

同样的错误,但奇怪的是,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

【问题讨论】:

    标签: powershell jenkins ms-word


    【解决方案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 的答案。

    【讨论】:

    • 用错误输出更新了初始问题。我已经使用 Jenkins 使用的相同用户名/密码登录到远程框,并且能够毫无问题地运行脚本。
    • 非常感谢您提供的链接。我有 99% 的信心,我自己永远不会朝那个方向看。虽然这不是 DCOM 问题,但它是将桌面添加到修复它的 SysWow 目录(在该链接中提到)。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多