定义命令输出的第一行为变量。

huangapple go评论132阅读模式
英文:

Batch. How to define the first line of command output into a variable?

问题

I have a batch script that goes through a list of devices and fetches the password for each of them from KeePass, using KPScript. %Output% is supposed to contain the password.

KPScript command outputs:

password
OK: Operation completed successfully.

The code below echoes
OK: Operation completed successfully.

@for /F "tokens=1,2,3,4 skip=1 delims=," %%A in (C:\iplist.csv) do (
    @echo I am at %%A using login %%B

    for /F "delims=" %%G in (
        '%KPSCRIPT% -c:GetEntryString %DBFILE% -pw:xxxx -Field:Password -ref-Title:%%A -refx-Group:"SSH/WEB"'
    ) do set output=%%G
    echo !output!
)

I have tried:

  1. Using /p
) do set /p output=%%G

With this, the output first echoes the password and then stops until I press Enter. When I do that, it echoes the second line of KPScript output.

  1. Using goto
) do set output=%%G & goto :done
:done
echo !output!

echo %%A

But it breaks the for loop and is unable to use variables %%A and %%B. Using files is not an option as I'm dealing with passwords.

英文:

I have a batch script that goes through a list of devices and fetches password for each of them from keepass, using KPScript. %Output% is supposed to contain the password.

KPScript command outputs:

password
OK: Operation completed successfully.

The code below echoes
OK: Operation completed successfully.

@for /F "tokens=1,2,3,4 skip=1 delims=," %%A in (C:\iplist.csv) do (
	@echo I am at %%A using login %%B

    for /F "delims=" %%G in (
		'%KPSCRIPT% -c:GetEntryString %DBFILE% -pw:xxxx -Field:Password -ref-Title:%%A -refx-Group:"SSH/WEB"'
	) do set output=%%G
    echo !output!

I have tried

  1. Using /p
) do set /p output=%%G

with this the output first echoes the password and then stops until I press Enter. When I do that, it echoes the second line of KPScript output.

  1. Using goto
) do set output=%%G & goto :done
:done
echo !output!

echo %%A

But it breaks the for loop and is unable to use variables %%A and %%B. Using files is not an option as I'm dealing with passwords.

答案1

得分: 0

@for /F "tokens=1,2,3,4 skip=1 delims=," %%A in (C:\iplist.csv) do (
    @echo 我在 %%A 处使用登录 %%B
    SET "output="

    for /F "delims=" %%G in (
        '%KPSCRIPT% -c:GetEntryString %DBFILE% -pw:xxxx -Field:Password -ref-Title:%%A -refx-Group:"SSH/WEB"'
    ) do if not defined output set "output=%%G"
    echo !output!
)
英文:
@for /F "tokens=1,2,3,4 skip=1 delims=," %%A in (C:\iplist.csv) do (
    @echo I am at %%A using login %%B
    SET "output="

    for /F "delims=" %%G in (
        '%KPSCRIPT% -c:GetEntryString %DBFILE% -pw:xxxx -Field:Password -ref-Title:%%A -refx-Group:"SSH/WEB"'
    ) do if not defined output set "output=%%G"
    echo !output!
)

for each line in the .csv, set the content of output to nothing then execute the kpscript. This has 2 lines. output is not defined, so it's set to the password. output is now defined, so the set out... is skipped.

huangapple
  • 本文由 发表于 2023年5月10日 19:34:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定