如何将带空格的路径传递给脚本 [英] How to pass path with spaces to script

查看:60
本文介绍了如何将带空格的路径传递给脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PowerShell调用位于包含空格的位置/路径的EXE。当我从命令行调用脚本时,EXE的完整路径没有传递给脚本。关于为什么发生这种情况的任何想法吗?

I am trying to use PowerShell to call an EXE that is at a location/path containing spaces. When I call the script from the command line, the EXE's full path is not being passed to the script. Any ideas as to why this is happening?

PowerShell脚本内容(Untitled1.ps1)

这是从命令行调用的整个脚本:

Here is the entire script that gets called from the command line:

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脚本的父文件夹运行的命令行字符串:

Here is the command line string being run from the PowerShell script's parent folder:

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 .\Untitled1.ps1 -NonInteractive -ParamExePath "C:\path with spaces\myapp.exe"

为此:

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

-命令用于执行命令的参数,例如{Get-Date}

The -Command Parameter used to execute commands for example {Get-Date}

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

The -File Parameter used to Run .ps1 Script 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 /?可获得完整的详细信息每个参数上

Type Powershell /? to get full details on each Parameter

这篇关于如何将带空格的路径传递给脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆