英文:
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:
- 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.
- 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
- 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.
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论