简体   繁体   中英

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. 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.

I would like to achieve the same result when calling PowerShell from cmd / task scheduler.

For example,

powershell notepad.exe "SampleParam".length

But when I do that I get "SampleParam".length literally instead of the "SampleParam".length calculated value.

The expected result was:

running 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 -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:

powershell -Command notepad.exe 'SampleParam'.length

But this will not:

powershell -Command notepad.exe "SampleParam".length

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM