Запуск сценария PowerShell из Jenkins приводит к InvokeMethodOnNull - PullRequest
0 голосов
/ 06 апреля 2020

При запуске приведенного ниже кода в любой IDE или консоли код выполняется без каких-либо исключений, но когда я пытаюсь запустить myScript.py в качестве шага сборки в Jenkins, он приводит к InvokeMethodOnNull и не может обработать какой-либо документ .

myScript.py

import os

SOURCE = 'path/to/source'
DESTINATION = 'path/to/destination'

os.system(f"powershell.exe myScript.ps1 '{SOURCE}' '{DESTINATION}'")

myScript.ps1

$Source = $args[0]
$Destination = $args[1]

if ($Source)
{
    if ($Destination)
    {
        New-Item -ItemType Directory -Force -Path $Destination
        $Source = Resolve-Path $Source
        $Destination = Resolve-Path $Destination
        $wordApp = New-Object -ComObject Word.Application
        Write-Host "Loaded MS Word"
        Get-ChildItem -Path $Source -Filter *.doc? | ForEach-Object {
            $document = $wordApp.Documents.Open($_.FullName)
            $pdfFilename = "$( $Destination )\$( $_.BaseName ).pdf"
            $tableOfContents = $document.TablesOfContents
            $tableOfContents.item(1).update()
            $document.SaveAs($pdfFilename, [ref]17)
            $document.Save()
            $document.Close()
            Write-Output "Processed $( $_.BaseName )"
        }
        $wordApp.Quit()
    }
    else
    {
        Write-Host 'Path to the destination folder is missing'
    }
}
else
{
    Write-Output 'Path to the source folder is missing'
}

Вот фрагмент журнала ошибок:

You cannot call a method on a null-valued expression.

+             $tableOfContents.item(1).update()
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

I Предположим, что проблема вызвана правами доступа, но могут быть и другие причины.

...