【问题标题】:How to pass path with spaces to script如何将带空格的路径传递给脚本
【发布时间】:2015-09-01 19:25:50
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: powershell command-line


    【解决方案1】:

    只要改变这个:

    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 用于执行命令的参数,例如 {Get-Date}

    用于运行 .ps1 脚本文件的 -File 参数

    -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 /? 以获取每个参数的完整详细信息

    【讨论】:

      【解决方案2】:

      其他几种称呼方式:

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

      【讨论】:

        【解决方案3】:

        请注意,如果您将文件夹路径传递给带有尾部反斜杠的 Powershell,则它无法处理它。例如-ParamFolder "C:\project 文件夹\app\bin 调试\"。参数字符串以双引号结尾。因此,当您尝试将文件名附加到它时,您最终会得到类似 C:\project folder\app\bin debug"Filename.txt。在这种情况下,您必须在末尾发送第二个反斜杠。

        【讨论】:

          猜你喜欢
          • 2019-06-30
          • 1970-01-01
          • 2019-07-02
          • 1970-01-01
          • 1970-01-01
          • 2020-11-02
          • 1970-01-01
          • 1970-01-01
          • 2010-10-03
          相关资源
          最近更新 更多