[英]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使用-Command参数:
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.