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

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

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:

  1. password
  2. OK: Operation completed successfully.

The code below echoes
OK: Operation completed successfully.

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

I have tried:

  1. Using /p
  1. ) 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
  1. ) do set output=%%G & goto :done
  2. :done
  3. echo !output!
  4. 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:

  1. password
  2. OK: Operation completed successfully.

The code below echoes
OK: Operation completed successfully.

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

I have tried

  1. Using /p
  1. ) 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
  1. ) do set output=%%G & goto :done
  2. :done
  3. echo !output!
  4. 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

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

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:

确定