我想使用PowerShell來調用位於包含空格的位置/路徑的EXE。當我從命令行調用腳本時,EXE的完整路徑不會被傳遞給腳本。任何想法爲什麼發生這種情況?如何將空間傳遞給腳本
PowerShell腳本內容(Untitled1.ps1)
這裏是獲取從命令行調用整個腳本:
param(
[string] $ParamExePath
)
function Run-CallThisExe {
param(
[string] $ThisExePath
)
Write-Host "ThisExePath: " "$ThisExePath"
start-process -FilePath $ThisExePath
}
write-host "ParamExePath: " $ParamExePath
Run-CallThisExe -ThisExePath "$ParamExePath"
命令行字符串
這裏是命令行字符串從PowerShell腳本的父文件夾運行:
powershell -command .\Untitled1.ps1 -NonInteractive -ParamExePath "C:\path with spaces\myapp.exe"
輸出
下面是運行腳本
ParamExePath: C:\path
ThisExePath: C:\path
start-process : This command cannot be run due to the error: The system cannot
find the file specified.
At C:\sample\Untitled1.ps1:11 char:5
+ start-process -FilePath $ThisExePath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand