2

I am trying to pass variable from VBscript to batch file for further usage. Since this value is a password, I dont want it to be echoed for security reasons!

Code of VBScript:

...
'after the above procedure, password has to be passed to batch file 
WScript.echo password

code for batch file:

FOR /F "usebackq tokens=*" %%r in (`CSCRIPT "D:\PasswordCreation.vbs"`) DO SET PASSWORD=%%r
ECHO %PASSWORD%
Pause

This stores the password in variable PASSWORD but the VBscripts pops out the password because of WScrip.echo password. Can i pass this password Without echoing? Is there some other command for that?

Anant Vaibhav
  • 325
  • 3
  • 6
  • 20

2 Answers2

2

You are not seeing the output from VBS - the FOR /F batch statement is absorbing that output.

The screen output is the result of ECHO %PASSWORD% in your batch script. Simply remove that line.

dbenham
  • 127,446
  • 28
  • 251
  • 390
1

I present a new element to use in your batch when you want to hide the entry of the password entered by the user and can take it as a variable for later use. So, this program is a hybrid that combines the script Batch, VBScript and HTA.

HTAPASSWORDBOX.bat

@echo off
Title G‚n‚rer un HTABOX pour cacher un mot de passe en ligne de commande Copyright Hackoo 2014
mode con cols=90 lines=3 & color 9B
Set MyVBSFile=%tmp%\%~n0.vbs
Set MyHTAFile=%tmp%\%~n0.hta
:: Créer le VBS Pour extraire et générer le code du HTA
Call :CreateMyVBS
:: Lancer Le VBS
Cscript.exe //NOLOGO %MyVBSFile%
:: Lancer Le HTA crée par le VBS
start /wait mshta.exe "%MyHTAFile%"
Del "%MyVBSFile%" & Del "%MyHTAFile%"
:: Lire le contenu du fichier %tmp%\userIn pour extraire le mot de passe tapé depuis
:: le HTABOX et le définir comme une variable
for /f %%i in (%tmp%\userIn) do set "Mypassword=%%i"
echo Votre mot de passe saisi est : %MyPassword%
Del %tmp%\userIn
pause 
Exit /b
:#Start
<html>
<head>
<title>Mot de Passe © Hackoo</title>
<hta:application id="htaid"
applicationName="Password"
border="thin"
icon="wlrmdr.exe"
borderStyle="normal"
caption="yes"
contextMenu="no"
maximizeButton="no"
minimizeButton="yes"
navigable="yes"
showInTaskbar="yes"
singleInstance="yes"
sysmenu="yes"
SCROLL="NO" 
SHOWINTASKBAR="Yes"   
SELECTION="no"
MINIMIZEBUTTON="no" 
>
</head>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES"> 
<BODY TOPMARGIN="1" LEFTMARGIN="1"><CENTER><DIV><SPAN ID="ONSCR"></SPAN></DIV></CENTER></BODY>
<script language="vbscript">
'---------------------------------------------------------------------------------------
Sub Window_OnLoad
    CenterWindow 300,150
    Call PasswordForm()
    Call TextFocus()
end sub
'---------------------------------------------------------------------------------------
Sub CenterWindow(x,y)
    Dim iLeft,itop
    window.resizeTo x,y
    iLeft = window.screen.availWidth/2 - x/2
    itop = window.screen.availHeight/2 - y/2
    window.moveTo ileft,itop
End Sub
'----------------------------------------------------------------------------------------
Sub SavePassword()
    set fs=CreateObject("Scripting.FileSystemObject")
    strFile=fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2),"UserIn"))
    set ts=fs.OpenTextFile(strFile,2,True)
    If PasswordArea.value <> "" Then
        ts.WriteLine PasswordArea.value
        ts.Close
        self.Close 'Pour fermer ce HTA après avoir enregistré le mot de passe comme une variable dans le fichier Userin
    else
        Msgbox "Le mot de passe est vide ! "& Vbcrlf & "SVP entrez de nouveau votre mot de passe",VbExclamation,"Mot de Passe © Hackoo"
        Location.reload(true) 'Pour recharger à nouveau ce HTA
    end if
End Sub
'----------------------------------------------------------------------------------------
Sub PasswordForm()
    Self.document.title = "Mot de Passe © Hackoo"
    Self.document.bgColor = "lightblue"
    ONSCR.InnerHTML="<center><FONT COLOR=""#FFFFFF"" SIZE=""+1"" FACE=""VERDANA,ARIAL,HELVETICA,SANS-SERIF"">Taper votre mot de passe</FONT<br>"_
    &"<input type=""password"" name=""PasswordArea"" size=""20"" onKeyUp=""TextFocus""><P>"_
    &"<input type=""Submit"" STYLE=""HEIGHT:25;WIDTH:110"" value=""OK"" onClick=""SavePassword"">"
END Sub
'----------------------------------------------------------------------------------------
Sub TextFocus
    PasswordArea.Focus 
End Sub
'----------------------------------------------------------------------------------------
</script>
</body>
</html>
:#End
::***********************************************************************************************
:CreateMyVBS
::'**********************************************************************************************
(
echo. Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo. Set f=fso.opentextfile^("%~f0",1^)
echo. a=f.readall
echo. Set r=new regexp
echo. r.pattern = "(?:^|(?:\r\n))(?::#Start\r\n)([\s\S]*?)(?:\r\n)(?::#End)"
echo. Set Matches = r.Execute^(a^)
echo. If Matches.Count ^> 0 Then Data = Matches^(0^).SubMatches^(0^)
echo. WriteFileText "%MyHTAFile%",Data
echo. f.close
::'**********************************************************************************************
echo. 
echo. Function WriteFileText^(sFile,Data^)
echo.     Dim objFSO,oTS,sText
echo.     Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo.     Set oTS = objFSO.CreateTextFile^(sFile,2^)
echo.     oTS.WriteLine Data
echo.     oTS.close
echo.     set oTS = nothing
echo.     Set objFSO = nothing
echo. End Function 
) > %MyVBSFile%
::'***********************************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70