PowerShell 标准输出重定向问题

问题描述 投票:0回答:1

我正在尝试使用 Ghostscript 来获取 PDF 中的页数。我收到此错误:

Select-Object : Exception getting "StandardOutput": "StandardOut has not been redirected or the process hasn't started yet."
At C:\Temp\Gainesville\EHADocumentExtractor\EHA_Extractor_ghostscript.ps1:156 char:101
+ ... Window -PassThru | Select-Object -wait -ExpandProperty StandardOutput
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: (System.Diagnostics.Process (gswin64c):PSObject) [Select-Object], GetValueInvocationException
    + FullyQualifiedErrorId : PropertyEvaluationExpand,Microsoft.PowerShell.Commands.SelectObjectCommand

下面是我的脚本

$gsPath = "C:\Program Files\gs\gs9.53.3\bin\gswin64c.exe"
  $pdfPath = "C:\Temp\Test.pdf"
  $gsCommands = @"
-q -dNODISPLAY -c \"($pdfPath) (r) file runpdfbegin pdfpagecount = quit\"
"@
    #$pageCount = & $gsPath -c $gsCommand 2>&1
    $pageCount = Start-Process -FilePath $gsPath -ArgumentList $gsCommands -NoNewWindow -PassThru | Select-Object -wait -ExpandProperty StandardOutput
Write-Host $pageCount

我不知道如何解决这个问题。在 C# 程序中,我可以更改 Process 对象的

RedirectStandardOutput
,但我不知道如何在 PowerShell 中解决这个问题。

powershell
1个回答
0
投票

您正在尝试从远程 PDF 获取页数。您正在使用旧的建议(我希望不是来自聊天机器人)。

Artifex 必须更改一些命令行功能,以减少流氓 PDF 漏洞的利用,因此您需要确保该文件夹位于权限列表中。文件名还需要区分大小写和 posix 风格。

因此,通过 Windows shell 的最短当前程序命令行将类似于:-

gs.lnk -q -Ic:/temp -dNODISPLAY -c "(c:/temp/test.pdf) (r) file runpdfbegin pdfpagecount = quit"

其中 gs.lnk 是我到 GSWIN32C.exe 的路径。因此,最小预期结果为

1

但是你提到使用 c#,因此最短的程序行应该是

int page_number = m_ghostscript.GetPageCount("my_document.pdf");

根据文档https://ghostscript.readthedocs.io/en/latest/LanguageBindingsCSharp.html#getpagecount

© www.soinside.com 2019 - 2024. All rights reserved.