首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带2对引号的Powershell cmd /c在Win 7上工作,但没有Win 10

带2对引号的Powershell cmd /c在Win 7上工作,但没有Win 10
EN

Stack Overflow用户
提问于 2019-02-25 16:48:18
回答 1查看 459关注 0票数 1

当我运行这个Powershell命令时,它在我的Windows 7 PC上工作,但在我的Windows 10 PC上却不能工作,为什么呢?

代码语言:javascript
运行
复制
cmd /c "`"`"C:\Program Files (x86)\National Instruments\LabVIEW 2018\LabVIEW.exe`"`""

错误是

代码语言:javascript
运行
复制
'""C:\Program' is not recognized as an internal or external command, operable program or batch file.

命令

代码语言:javascript
运行
复制
cmd /c "`"C:\Program Files (x86)\National Instruments\LabVIEW 2018\LabVIEW.exe`""

这两种方法都可以使用,但我必须添加另一对引号,因为我的命令实际上包含路径:

代码语言:javascript
运行
复制
cmd /c "`"`"C:\Program Files (x86)\National Instruments\LabVIEW 2018\LabVIEW.exe`" `"$runner_File`" > `"$runnerResult_File`"`""
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-25 17:01:54

避免引用头痛

  • 通过让PowerShell直接处理控制台应用程序的调用
  • 通过仿真cmd.exe>重定向和Set-Content -Encoding Oem
代码语言:javascript
运行
复制
& "C:\Program Files (x86)\National Instruments\LabVIEW 2018\LabVIEW.exe" $runner_File |
  Set-Content -Encoding Oem $runnerResult_File

请继续阅读以得到解释。

如果您确实想用cmd /c解决最初的问题,请参阅下面的部分。

  • 通常不需要使用cmd /c从PowerShell调用控制台应用程序。
代码语言:javascript
运行
复制
- Piping to `Set-Content -Encoding Oem` can be used to emulate `cmd.exe`'s `>` output redirection, though note that this may slow things down.
代码语言:javascript
运行
复制
    - On the plus side, `Set-Content` allows you to _control_ the output encoding.

代码语言:javascript
运行
复制
- To control what character encoding PowerShell assumes the console application's output uses, set `[Console]::OutputEncoding` _before_ invoking the application.
- While you could use PowerShell's own `>`, it would create UTF-16LE files by default in Windows PowerShell (and BOM-less UTF-8 files in PowerShell _Core_) - though in PSv5.1+ you can [change the encoding that `>` uses](http://stackoverflow.com/a/42451413/45375) via the `$PSDefaultParameterValues` dictionary of presets.

  • 若要调用需要引用路径或通过变量提供路径的控制台应用程序,请使用调用操作符&
  • 如果您通过变量(例如,$runnerFile)提供参数,PowerShell将在需要时在幕后为您引用变量值。

cmd /c解决问题

奇怪的是,将包含多个cmd /c参数的命令行传递给 "..."**-enclosed ,需要在** "...".中封装_E 251完整的E 152>E 253命令行。

以下是如何通过PowerShell实现这一目标:

  • 将单独转义的"实例(`")放在传递给cmd /c的命令行的两端,这将成为整个"..."的封闭部分。
  • 否则,依赖于PowerShell自己的幕后按需引用可变值。
代码语言:javascript
运行
复制
cmd /c `" "C:\Program Files (x86)\National Instruments\LabVIEW 2018\LabVIEW.exe" $runner_File > $runnerResult_File `"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54870984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档