2015-09-01 112 views
3

我想使用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 

回答

5

只要改變這之後的是輸出:

powershell -command .\Untitled1.ps1 -NonInteractive -ParamExePath "C:\path with spaces\myapp.exe" 

這樣:

powershell -file .\Untitled1.ps1 -NonInteractive -ParamExePath "C:\path with spaces\myapp.exe" 

-Command參數用於執行命令,例如{獲取最新}

-File參數用於運行的.ps1腳本文件

-Command 
    Executes the specified commands (and any parameters) as though they were 
    typed at the Windows PowerShell command prompt, and then exits, unless 
    NoExit is specified. The value of Command can be "-", a string. or a 
    script block. 

-File 
    Runs the specified script in the local scope ("dot-sourced"), so that the 
    functions and variables that the script creates are available in the 
    current session. Enter the script file path and any parameters. 
    File must be the last parameter in the command, because all characters 
    typed after the File parameter name are interpreted 
    as the script file path followed by the script parameters. 

類型Powershell /?讓每個參數的全部細節

1

幾種其他方式稱之爲:

powershell -command .\Untitled1.ps1 -NonInteractive "-ParamExePath 'C:\path with spaces\myapp.exe'" 

powershell -command ".\Untitled1.ps1 -ParamExePath 'C:\path with spaces\myapp.exe'" -NonInteractive