A
A
AlexChubukov2019-08-21 18:54:09
cmd/bat
AlexChubukov, 2019-08-21 18:54:09

Why is cmd converting string to bool?

I want to write a bat file that finds the username, password and domain in the json file, and then writes them to local variables and runs the program with certain rights. The format in which the login and password are given is the simplest
"Login": "login",
"Password": "password"
Wrote the following script. The login is determined correctly, however, when determining the password and domain, after executing the for loop, they are assigned boolean values.
The following lines are displayed in succession
set password="password",
set password=false,
The comma here is saved from the json file. With what it can be connected? For the first time there was a need to write something in cmd, so do not judge strictly by the code. I know there are ready-made solutions for parsing json files from cmd and powershell, but I don’t want to introduce additional dependencies, since I won’t be running it on my computer.

echo on
:: Search for login
set login 
FOR /F "tokens=2 delims= " %%i IN ('findstr Login configuration.json') DO ( set login=%%i )
set login=%login:~1,-3%
echo %login%
:: Search for password
set password
FOR /F "tokens=2 delims= " %%j IN ('findstr Password configuration.json') DO ( set password=%%j )
set password=%password:~1,-3%
echo %password%
:: Search for Domain
set domain
FOR /F "tokens=2 delims= " %%k IN ('findstr Domain configuration.json') DO ( set domain=%%k )
set domain=%domain:~1,-3%
echo %domain%


if not defined login (
  if not defined password (
    %~dp0application.exe
  ) else (
    echo %password% | runas /user:%domain%\%login% %~dp0application.exe
  )
) else (
    echo %password% | runas /user:%domain%\%login% %~dp0application.exe
  ) 

pause

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sergey, 2019-08-22
kuzmin @sergueik

AlexChubukov if not jqthen turned convertfrom-json
https://smearg.wordpress.com/2014/07/02/work-with-j...

$a = @'
{
  "Login": "Alex" ,
  "Password": "qwerty" ,
  "Domain": "Alex" ,
  "Output_json": "json_files" ,
  "Output_log": "logs"
}
'@

$a | convertfrom-json 

Login       : Alex
Password    : qwerty
Domain      : Alex
Output_json : json_files
Output_log  : logs

if only not cmd

W
wisgest, 2019-08-23
@wisgest

1) The easiest way is not to use CMD, but to use WSH / JS (a standard Windows tool) evalto parse JSON.
2) This should not work: (see Entering a password for RUNAS ). Total (works for me):

0</* :
 @echo off
 set COMMAND="%~dp0application.exe"
 CScript //e:JScript //nologo //t:0 //i "%~f0" <configuration.json
 exit /b
*/0;
var conf = eval("(" + WScript.StdIn.ReadAll() + ")");
var WshShell = new ActiveXObject("WScript.Shell");
var Cmd = WshShell.Exec("cmd /q echo off");
var CmdIn = Cmd.StdIn;
var WinTitle = "rnd" + Math.random();
CmdIn.WriteLine("title " + WinTitle);
while (!WshShell.AppActivate(WinTitle)) WScript.Sleep(1);
WshShell.SendKeys("*~");
CmdIn.WriteLine("doskey /exename=runas.exe *=" + conf.Password);
CmdIn.WriteLine("start /b runas /user:"
 + conf.Domain + "\\" + conf.Login
 + " %COMMAND%>con 2>&1 <con"); // RUNAS <CON -- важно!
CmdIn.WriteLine("exit");
Cmd.Stdout.ReadAll();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question