中执行带有参数的外部程序

中执行带有参数的外部程序

本文介绍了如何在 PowerShell 中执行带有参数的外部程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读此答案stackoverflow 回答,它让我成功了一半.这是我需要做的.

I have read this answer stackoverflow answer and it get's me there half way. Here is what I need to do.

执行这个命令:

"c:\myexe.exe <c:\Users\Me\myanswerfile.txt"

如果我直接在我的 powershell 脚本中运行它

If I run that straight from within my powershell script

&'c:\myexe.exe <c:\Users\Me\myanswerfile.txt'

我收到此错误:

The term 'C:\myexe.exe <c:\Users\Me\myanswerfile.txt' is not recognized as the name of
a cmdlet, function, script file, or operable program. Check the spelling of the name,or
if a path was included, verif that the path is correct and try again.

现在我已经尝试了几种变体,包括将原始命令放在一个名为 $cmd 的变量中,然后传递

如果我附加<"对于 $cmd 变量,该命令失败,并出现与第一个类似的错误.

Now I have tried several variations of this including placing the original command in a variable called $cmd and then passing the

If I append the '<' to the $cmd variable the command fails with a similar error as the first one.

我被难住了.有什么建议吗?

I'm stumped. Any suggestions?

推荐答案

如果你想运行一个程序,只需输入它的名称和参数:

If you want to run a program, just type its name and parameters:

notepad.exe C:\devmy\hi.txt

如果您想运行一个 exe 并将 stdin 重定向到您的示例似乎是尝试的它,请使用:

If you want to run an exe and redirect stdin to it which your example seems to be an attempt of, use:

Get-Content c:devmy\hi.txt | yourexe.exe

如果您需要指定程序的完整路径,那么您需要使用与号和引号,否则 powershell 认为您正在定义一个普通字符串:

If you need to specify the full path to the program then you need to use ampersand and quotes otherwise powershell thinks you are defining a plain string:

&"C:\Program Files (x86)\Notepad++\notepad++.exe"

这篇关于如何在 PowerShell 中执行带有参数的外部程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:10