【问题标题】:PowerShell executing exe with argumentsPowerShell 执行带有参数的 exe
【发布时间】:2018-10-22 15:34:25
【问题描述】:

我编写了一个 PowerShell 脚本来执行带有空格的 exe,但它一直失败,没有读取完整路径。

$now     = Get-Date -format "MM_dd_yyyy"
$onepath ="C:\Program Files (x86)\GoFileRoom\IWMUploadDocuments\logs\"
$scpath  = "F:\Program Files\NSClient++\scripts\"
$onefile = "IWMUploadDocumentsLog$now.txt"
$script  = "check_log3.exe"

& "$scpath$script" -p "Error Logging into GFR" -l "$onepath$onefile" -c 1 
Write-Output "$onepath$onefile"

这是输出:

PS C:\Windows\system32> F:\Program Files\NSClient++\scripts\onesource.ps1 无法读取“C:\Program” C:\Program Files (x86)\GoFileRoom\IWMUploadDocuments\logs\IWMUploadDocumentsLog10_22_2018.txt

【问题讨论】:

  • 尝试使用内撇号˙"'$onepath$onefile'"˙ 或双引号作为"""""$onepath$onefile"""""

标签: powershell spaces


【解决方案1】:

在我看来,您需要在参数-l 的参数周围加上一组额外的引号:

& "$scpath$script" -p "Error Logging into GFR" -l "`"$onepath$onefile`"" -c 1

或者试试splatting参数:

$params = '-p', 'Error Logging into GFR',
          '-l', "$onepath$onefile",
          '-c', 1

& "$scpath$script" @params

【讨论】:

  • 在 PowerShell 控制台中手动调用扩展命令(不使用变量)是否有效?如果您在 CMD 窗口中执行相同操作,它是否有效?
  • PS F:\Program Files\NSClient++> .\scripts\check_log3.exe -p "登录 GFR 时出错" -l "C:\Program Files (x86)\GoFileRo om\IWMUploadDocuments\logs \IWMUploadDocumentsLog06_22_2018.txt" 无法读取 'C:\Program'
  • 那么问题出在您正在调用的命令上,而不是 PowerShell 上。尝试使用短路径,或将文件放在没有空格的路径中。
  • 当我将文件移动到非空间文件夹时它工作正常。
猜你喜欢
  • 2016-07-26
  • 1970-01-01
  • 2023-03-15
  • 2017-08-22
  • 1970-01-01
  • 2013-12-27
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多