首页 > 解决方案 > PowerShell 执行带有参数的 exe

问题描述

我编写了一个 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

标签: powershellspaces

解决方案


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

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

或者尝试喷溅论点:

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

& "$scpath$script" @params

推荐阅读