提问人:mashiro ginca 提问时间:11/8/2023 最后编辑:mklement0mashiro ginca 更新时间:11/9/2023 访问量:39
使用 powershell 将参数传递给 .exe
Passing a parameter to .exe, using powershell
问:
需要通过 PowerShell 运行执行数组排序的控制台应用程序 .exe 50 次,以找出应用程序的平均运行时间。我希望应用程序的输入是自动的。但这行不通。怎么了?
代码 powershell:
$asm_average=0
$cycles = 50
FOR ($i = 1; $i -lt $cycles; $i++)
{
echo "ASM CODE:"
$x = (get-date).ToString('yymmddhhmmssfff')
$o = Get-Random -InputObject (1..100) -Count 30
$c = ($o -join ' ').ToString()
D:\lr\projects\helloworld\pp2.exe "$c" $wshell.SendKeys('~') -Wait ??
$y = (get-date).ToString('yymmddhhmmssfff')
$y = $y - $x
echo "TIME:", $y
echo ""
$asm_average = $asm_average + $y
}
$asm_average = $asm_average / $cycles
echo "ASM AVERAGE TIME:", $asm_average
如何工作执行: 输入数组: 5 8 9 3 4 2 0 66 排序数组:0 2 3 4 5 8 9 66
答:
0赞
mklement0
11/8/2023
#1
看起来您正在尝试自动响应外部程序呈现的交互式提示:
最好的方法是调查是否有办法避免此类提示,如果可能的话,使用目标程序支持的参数(例如自动确认是/否提示,或接受否则会提示的参数的参数)。
-y
下一个最佳方法是尝试通过 stdin 提供自动响应,在 PowerShell 中,这意味着将它们通过管道传递到目标程序;例如:
# Equivalent to pressing ENTER when pp2.exe presents an interactive prompt. '' | D:\lr\projects\helloworld\pp2.exe "$c"
这假设目标程序从 stdin(标准输入流)读取用户输入,除非重定向,否则 stdin 会从终端(控制台)请求交互式用户输入。
但是,程序可以选择显式地从终端(控制台)读取,而与 stdin 无关。
最不理想的方法是通过发送击键来模拟交互式用户输入,这本质上是脆弱的:
# Simulate sending an ENTER keypress to the target program. (New-Object -ComObject Wscript.Shell).SendKeys('~') D:\lr\projects\helloworld\pp2.exe "$c"
- 上面假设目标程序接受缓冲键盘输入;如果没有,则需要做更多的工作 - 请参阅此答案。
评论