【问题标题】:Powershell - how to pass variables within an argument listPowershell - 如何在参数列表中传递变量
【发布时间】:2016-09-28 11:24:37
【问题描述】:

我有一个使用命令行应用程序生成 PDF 报告的脚本。

这是批处理形式的脚本(日期是变量):

QsrCrExport.exe -f"C:\SMTP\Reports\Speed of Service Summary" -o"C:\SMTP\Daily_Archive\SpeedofService.pdf" -a"Date:%one%,%one%" -epdf

我正在尝试使用带参数的启动进程将上述内容实现到 Powershell 中,但似乎无法让参数正常工作

$crexport = "C:\Program Files (x86)\ReportViewer\Bin\QsrCrExport.exe";
$arguments = '-f"C:\ProgramData\ReportViewer\Reports\Speed of Service Summary" -o"C:\SMTP\Generated\SpeedofService.pdf" -a"Date:$reportdate1,$reportdate2" -epdf'
Start-Process $crexport $arguments -Wait;

我知道这在单引号中不起作用,因为变量不会被值替换,但是如果我删除参数中的所有双引号并将整行包裹在双引号中,它仍然不起作用.

我对 powershell 还很陌生,如果这真的很简单,我深表歉意。

【问题讨论】:

  • stackoverflow.com/questions/38900961/…。这个很弱,但答案是正确的。您需要将参数作为数组传递。如果您同意,将标记为受骗。还有其他关于调用 exe 的答案,但对于没有被埋没的启动进程来说,这是我能接受的最好的答案。

标签: powershell


【解决方案1】:

您可以使用参数列表数组:

Start-Process -FilePath '"C:\Program Files (x86)\ReportViewer\Bin\QsrCrExport.exe"' -ArgumentList @('-f"C:\ProgramData\ReportViewer\Reports\Speed of Service Summary"', '-o"C:\SMTP\Generated\SpeedofService.pdf"', '-a"Date:$reportdate1,$reportdate2"', '-epdf') -wait

应该工作。

【讨论】:

    【解决方案2】:

    我看到的问题是单引号内有变量。我已经切换了引号,以便双引号内的所有内容都表示为对象而不是专用字符串。此外,通过在单引号内的字符串上使用 -f 运算符,您可以将变量传递到字符串中,即使在单引号内也是如此。

    $crexport = "C:\Program Files (x86)\ReportViewer\Bin\QsrCrExport.exe"
    $arguments = ("-f 'C:\ProgramData\ReportViewer\Reports\Speed of Service Summary' -o 'C:\SMTP\Generated\SpeedofService.pdf' -a 'Date:{0},{1} -epdf'" -f $reportdate1, $reportdate2)
    Start-Process $crexport $arguments -Wait
    

    【讨论】:

      猜你喜欢
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2016-10-02
      • 2023-03-08
      • 2021-11-03
      • 2018-02-17
      • 1970-01-01
      相关资源
      最近更新 更多