简体   繁体   English

从powershell.exe命令调用程序并操纵参数

[英]Call program from powershell.exe command and manipulate parameters

I'm trying to call an EXE file program that accepts command line parameters from PowerShell. 我正在尝试调用一个从PowerShell接受命令行参数的EXE文件程序。 One of the parameters I'm required to send is based on the string length of the parameters. 我需要发送的参数之一是基于参数的字符串长度。

For example, 例如,

app.exe /param1:"SampleParam" /paramLen:"SampleParam".length

When I run the above, or for example: 当我运行上面的内容时,例如:

notepad.exe "SampleParam".length

Notepad opens with the value 11 as expected. 记事本将按预期值打开11。

I would like to achieve the same result when calling PowerShell from cmd / task scheduler. 从cmd /任务计划程序调用PowerShell时,我想获得相同的结果。

For example, 例如,

powershell notepad.exe "SampleParam".length

But when I do that I get "SampleParam".length literally instead of the "SampleParam".length calculated value. 但是,当我这样做时,我从字面上得到了"SampleParam".length而不是"SampleParam".length计算值。

The expected result was: 预期结果是:

running notepad.exe 11 运行notepad.exe 11

I'd suggest using variables to store your string, etc. 我建议使用变量来存储您的字符串等。

$Arg1 = 'SampleParam'
## This will try to open a file named 11.txt
powershell notepad.exe $Arg1.Length

In your specific example: 在您的特定示例中:

app.exe /param1:$Arg1 /paramLen:$Arg1.Length

Utilizing splatting: 利用喷溅:

## Array literal for splatting
$AppArgs = @(
    "/param1:$Arg1"
    "/paramLen:$($Arg1.Length)"
)
app.exe @AppArgs

Use the -Command parameter for powershell.exe: 对Powershell.exe使用-Co​​mmand参数:

powershell -Command "notepad.exe 'SampleParam'.length"

Be careful with the "'s since they can be picked up by the Windows command processor. This will also work: 小心使用“”,因为它们可以被Windows命令处理器拾取。这也将起作用:

powershell -Command notepad.exe 'SampleParam'.length

But this will not: 但这不会:

powershell -Command notepad.exe "SampleParam".length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从powershell.exe向脚本传递参数 - passing parameters to script from powershell.exe 如何使用脚本块和参数将 powershell.exe 与 -Command 一起使用 - How to use powershell.exe with -Command using a scriptblock and parameters 切换参数和powershell.exe -File参数 - Switch parameters and powershell.exe -File parameter powershell.exe -file vs. -command - powershell.exe -file vs. -command 如何使@'...'@ 在 powershell.exe -command “”中工作? - How to make @'…'@ work in powershell.exe -command “”? powershell.exe -ex旁路-命令 - powershell.exe -ex bypass -command 对powershell.exe正确转义命令(从CMD.exe启动命令) - Escaping command correctly for powershell.exe (launch command from CMD.exe) 使用-Co​​mmand和-File开关从命令行(cmd.exe)运行Powershell.exe吗? - Run Powershell.exe from command line (cmd.exe) with -Command and -File switches? Powershell:命令不适用于“powershell.exe -command”命令 - Powershell: Command will not work with "powershell.exe -command "command" pipenv shell 在虚拟环境中启动子shell…“powershell.exe”不是内部或外部命令,也不是可运行的程序或 - pipenv shell Launching subshell in virtual environment… 'powershell.exe' is not recognized as an internal or external command, operable program or
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM