1

I want to execute some commands and download things with batch

There are multiple servers and I want to load balance it. There is also the reason for the network. So I want to test with tcping

There is a problem now that the return value of the for execution command cannot be obtained The tcping tool I use: https://github.com/cloverstd/tcping

code:

for /F %%a in (‘powershell ./tcping.exe -H https://a.server…’) do @set server1=%%a
echo “%server1%”

The content returned after execution: Minimum

Expected return: Minimum = 100.0000ms, Maximum = 100.000ms, Average = 100.0000ms

The value of for can only get the first one (Minimum ) Not all content returned because there is a space. The test is the same for other commands, and it will be terminated when a space is encountered.

So there is no way to get the latter value

I read Microsoft's doc, and command help, which doesn't mention more about what set returns for command

Is there any solution?


Also, I'm new to programming and it's not that long. Also not very good at using this community. If there is something wrong, please forgive me

aschipfl
  • 33,626
  • 12
  • 54
  • 99
uends
  • 11
  • 2
  • 2
    `for /f "delims=" %%a in ....` - see `for /?` – mklement0 Jul 20 '22 at 18:59
  • 4
    The utility `tcping.exe` - is it really necessary to execute it in a PowerShell environment? – Stephan Jul 20 '22 at 19:08
  • Oh, I did check the help before asking the question. However it appears Press any key to continue I thought its help content was only the first paragraph and did not continue to see the content behind So all I see is: FOR /F ["options"] %variable IN ('command') DO command [command-parameters] I've been bothered for a long time, maybe a little anxious I'm sorry I just started learning too Regarding whether it is necessary to use powershell, Because I'm used to using . . so Is there any harm in doing this? I don't understand yet – uends Jul 20 '22 at 19:22
  • 2
    You may get the same result in a much faster way if you just use `for /F "delims=" %%a in ('tcping.exe -H https://a.server...') do @set server1=%%a` – Aacini Jul 20 '22 at 23:15

2 Answers2

1
  • To make for /f report lines in full in a single variable instead of splitting them by whitespace and reporting each token in a separate variable, use "delims="

    • Run for /? for a full description of the for statement (this produces paged output, requiring a keypress to move to the next page).
  • As Stephan points out in a comment, you do not need PowerShell to call an external program such as tcping.exe - just call it directly, which also makes the invocation much faster.

Therefore, use the command Aacini suggests in a comment:

for /f "delims=" %%a in ('tcping.exe -H https://a.server…') do @set server1=%%a

Unless you're forced to use batch files and direct use of PowerShell isn't an option:

  • Consider implementing the functionality in a PowerShell script (.ps1) as proposed in Jim's answer.

  • For more information on the trade-offs between using pure batch-file solutions, hybrid batch-file solutions (batch files that call powershell.exe) and pure PowerShell solutions (.ps1 files), see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

Couple things:

  1. I would not use a batch command unless I absolutely had to. PowerShell is much easier to use than the cryptic nonsense created over 20 years ago.
  2. You could create a PowerShell .ps1 script and do the whole operation in native PowerShell and make it much easier on yourself.

If you are going to use PowerShell, try using Test-Connection instead of a command line app. It will provide you plenty of information you can base your logic on to make the operation more resilient.

Jim
  • 692
  • 7
  • 15
  • 5
    PowerShell scripts are certainly more powerful than batch files, but until the Operating System, is configured out of the box to run them, _(like it does batch files)_, I cannot agree that they are much easier. As it stands, the best way to run a powershell script on an unknown or non configured system, is from a batch file! – Compo Jul 20 '22 at 21:02
  • That really depends on the system. Current OS versions by default install windows PowerShell and associate the .ps1 extension with the command shell. Legacy systems do not. I've stopped using any OS that is out of support which rules out those that don't enable PowerShell support by default. – Jim Jul 20 '22 at 22:57
  • In corporate environments, largely run by non-technicians, it's common for Powersmell to be disabled as a matter of corporate policy. – Magoo Jul 20 '22 at 23:22
  • @Jim: I disagree with your _opinions_ on PowerShell vs. Batch-files. My opinion is that PowerShell is more difficult for the average user to learn and use than a Batch file! I compiled a series of experiences from other users that support such an opinion. If you are interested, I invite you to review _the comments_ below [this answer](https://stackoverflow.com/a/42603273/778560)... – Aacini Jul 20 '22 at 23:32
  • 1
    @Aacini, it's ok if you disagree. I've been a software engineer for over 30 years and comparing the syntax of a batch file etc. to that of PowerShell cmdlet and telling me PowerShell is harder to learn is hard to swallow. – Jim Jul 21 '22 at 13:07
  • 1
    @Magoo, true. It amusing to me they have this policy because a determined hacker can just as easily craft a cmd/batch file to do just as much damage. Companies I have worked at had this policy, until it was demonstrated that this policy was a placebo and they were limiting the ability of the IT dept to be innovative in providing services for the customers. Then they reversed this policy since it operates under the same permissions a batch file would and thus not a security vulnerability. – Jim Jul 21 '22 at 13:11