【问题标题】:PowerShell Start-Process -redirectStandardOutput throws: The system cannot find the file specifiedPowerShell Start-Process -redirectStandardOutput 抛出:系统找不到指定的文件
【发布时间】:2015-05-04 21:15:32
【问题描述】:

在我正在执行的 PowerShell 窗口中:

Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait 
-redirectStandardOutput "D:\\test1.txt"

得到:

Start-Process : This command cannot be executed due to the error: The system cannot find the file specified. At line:1 char:14 + Start-Process <<<< XXXXX.exe -ArgumentList "some valid arguments here" -redirectStandardOutput "D:\\test1.txt" + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

如果我省略 -redirectStandardOutput 参数(确实如此),我已检查启动过程是否按预期工作。

test1.txt 是一个新文件,不尝试追加。

令人惊讶的是,D:\ 上的 test1.txt 文件是在我运行该行时创建的,它仍然是空的。

有人知道这里发生了什么吗? 谢谢。

编辑

我发现如果我跑步:

Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait 
-redirectStandardOutput "D:\\test1.txt"

它失败了(如最初发布的那样) 如果我跑:

Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait 

它工作正常,但不会将控制台输出保存到文件中 如果我跑

Start-Process .\XXXXX.exe -ArgumentList "some valid arguments" -wait 
-redirectStandardOutput "D:\\test1.txt"

它的工作原理与预期的差不多。 那么为什么我在使用重定向时需要指定路径,而当我不使用时它运行得很好呢?

编辑 概括问题;关于当前目录中的脚本/exe 要求允许运行 ./ 前缀的要求似乎有些不一致。当我不重定向输出时, ./ 不是必需的。任何人都知道这是否是预期的行为?

【问题讨论】:

标签: powershell runtime-error start-process


【解决方案1】:

RichyRoo,

您看到的问题很可能是因为您的 XXXXX.exe 位置未在 PATH 环境变量中注册。

如果您尝试使用在 PATH 环境变量中定义的文件夹之一中注册的应用程序运行您的命令,它应该可以在没有尾随 .\ 即:

Start-Process cmd.exe -ArgumentList "/c ping 127.0.0.1" -redirectStandardOutput "D:\ABC\test.txt" -Wait

我使用示例批处理文件尝试了第二个示例,如果没有在路径前加上 .\,它就无法工作 - 它会复制您的行为。

所以它可以通过在你的 xxxxx.exe 前加上 .\ 来限定你的 .exe 位置,因为我假设你在 shell 中的当前目录是你的 .exe 位置。

我还没有查到为什么当前路径没有被查看命令执行级别。

【讨论】:

  • ps。我希望你不是在寻找一个快速的答案;)
猜你喜欢
  • 2021-11-25
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多