How to allow user input batch script & parse the value to cmd - windows

#echo off
title Remote Message
set /p input1= "Type the PC name: "
set pcname=%input1%
set /p input2=echo Type a message:
set msgmsg=%input2%
cmd msg /server:%pcname% */time:999 "%msgmsg%"
pause
Above is the batch script
how can I allow user input the pc name and the message he wanted to send remotely thru cmd? I tried 100times above code and keep modify but still not work.

No need to set variables from variables, and please do NOT name your batch file msg.bat or msg.cmd just call it something like send_message.cmd:
#echo off
title Remote Message
set /p "pcname=Type the PC name: "
set /p "msgmsg=Type a message: "
msg /server:%pcname% */time:999 "%msgmsg%"
pause

Related

How do you supress the user response in a cmd set prompt?

I want to have an authentication prompt for my batch file. So I used set /P like this:
set /P password=Enter password:
But the problem is that I have to suppress the user output like when using net user anyuser *. Is there a way to do that in Windows 10?
I tried set /P password="Enter password:" >NUL but it didn't work.
Thanks in Advance

Automating connection to VNC with batch file

I'm trying to create a batch file to automate vnc connections, this is what i came up with:
#echo off
:Begin
set "PASS=123"
set /p IP=Enter IP Address:
echo Connecting...
start /d "C:\Program Files (x86)\UltraVNC\" VNCVIEWER.EXE %IP%
goto Begin
problem is that the program comes up with a second pop up display for the password which is always 123 but i don't know how to make that automatic too, once the process is open how do i make the batch file enter the password as well automatically ?

Runas SC stop remote service with spaces

I am using the sc command to remotely restart a process under runas. This works perfectly with a service that has no spaces, but if it has spaces, forcing me to use ' inside the runas "command" quotes, it fails and cannot find the service.
I have already checked the properties of this particular service to ensure the service name (it is the same as the display name).
#ECHO OFF
REM Prompt for Domain & Username. Password will be prompted when run.
set /P Domainn=Enter Domain Name:
set /P Usern=Enter Username:
set service=workingservice
REM set service='Not a working service'
REM Define Start Stops
set userrunasstop=runas /profile /user:%domainn%\%usern% "sc \\%domainn% stop %service%"
set userrunasstart=runas /profile /user:%domainn%\%usern% "sc \\%domainn% start %service%"
:optionmenu
CLS
ECHO 1 - Stop Service
ECHO 2 - Start Service
ECHO q - Quit
ECHO.
set /P optionnum=Enter command number:
GOTO option%optionnum%
:option1
REM Stop Client
%userrunasstop%
goto optionmenu
:option2
REM Start Client
%userrunasstart%
goto optionmenu
:optionq
EXIT
This script works great as shown, but when I comment out set service=workingservice and un-comment set service='Not a working service it doesn't work. Also, if I use runas to open its open cmd.exe, I can then successfully run the sc command with quotes around the service name.
I have set up a workaround for this issue by opening up a new command prompt under runas. And in the new command prompt calling another bat file that executes the sc stop service command. This removes the multiple quoting that was causing the error.
I am still working on passing variables from the runas batch file to the sc batch file to allow the %domain% variable to be input into the sc command.
File runas.bat
set /P Domainn=Enter Domain Name:
set /P Usern=Enter Username:
runas /profile /user:%domainn%\%usern% "cmd sc.bat"
File sc.bat (minus all the frills in the original script)
set service='Service with spaces'
sc \\domainname stop %service%

>> Login screen : From Batch to VBscript and again to Batch <<

I am running a batch script and somewhere the user has to access a database.
At this moment, a window made in vbscript would prompt asking the user to type in the login and password. (OK, Cancel buttons)
If the credentials are correct after the OK, the batch would continue according to planA, otherwise the batch would do something else going to planB. If (Cancel), it would return to the batch and the menu above.
#echo off
:Ini
echo [1] Access database
echo [2] Main menu
echo:
set /p Quest= What do you prefer (1 / 2)?
if not '%Quest%'=='' set Quest=%Quest:~0,1%
if '%Quest%'=='1' goto VBS
if '%Quest%'=='2' goto BATCH
echo Invalid option, please try again
cls
goto Ini
:BATCH
echo Heading for main menu ...
goto Main
:VBS
cscript login.vbs
(...)
-- How to continue and make the vbs?
-- How to capture the user information, validate it and go back to the batch for the planA or planB ...
-- How to mask that password with ** ** ?
Help will be greatly appreciated !
Better switch to vbscript entirely (or since you seem new to vbscript another language more recent and powerfull while keeping it easy like Ruby). Everything you start from the batch can also be done in Vbscript, you can use prompt for the menu and inputbox for the password and if it has to be masked use a the browser as UI like the script from Rob Vanderwoude here http://www.robvanderwoude.com/vbstech_ui_password.php
Using this technique you can do all the UI/GUI in Internet Explorer and the logic in Vbscript.
If you decide to keep the batch approach, you can exit a vbs script with Wscript.Quit X, where x is the errorlevel you pass to windows when the script finishes, you can then trap that errorlevel in the batch. Alternative is to set or change an environment variable to do the trasfer of data, and last you can write data to textfiles easily in script and batch but the parsing of this in batch is more difficult.
I have found an intersting alternative as described below
http://www.computerhope.com/forum/index.php?topic=103686.0
VBSscript embeded in BATCH
#echo off
:wscript.echo InputBox("Enter your password","VBScript-Batch")
findstr "^:" "%~sf0" | findstr /i /v ":Label" >temp.vbs
for /f "delims=" %%N in ('cscript //nologo temp.vbs') do set pass=%%N
del temp.vbs
echo You entered %pass%
:Label1
echo continue from here
If %pass%=="ok" echo Valid Password ! & goto EOF
If %pass%=="ok" echo Invalid Password !! & goto EOF
:EOF
pause
As you see, if we eliminate the "& goto EOF" the script works well. It sends the VBS input "pass" to the batch and the batch echoes "continue from here", from where the rest of your code goes.
However, it is not working as it should. Any help to make this really work ?
Another alternative is ....
I have added in the existing VBSscript for "Internet Explorer version" the code :
VBS SCRIPT - named Password.vbs (see full script in the above link from Peter)
strPw = GetPassword( "Please, type your password:" )
WScript.Echo "Your password is: " & strPw
Sub Submit_OnClick
dim filesys, filetxt, FormContent
Set FormContent = document.getElementById("strPw")
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("C:\\temp.txt", 8, True)
filetxt.WriteLine(FormContent.value)
filetxt.Close
End Sub
BATCH SCRIPT
#echo off
cscript Password.vbs
findstr /B /E /M %strPw% temp.txt
If %ERRORLEVEL% EQU 0 echo Password matched! & goto EOF
If not %ERRORLEVEL% EQU 0 echo Invalid Password !! & goto EOF
:eof
pause
The file temp.TXT should be sent to the c:\ with the information the user typed on the inputbox. The batch would read this input and compare to a set password and continue the coding...
How can I make this work?? the temp.TXT is not generated an so forth ...
BATCH and VBS gurus out there, any help to solve these problems is really welcomed !

In Windows cmd, how do I prompt for user input and use the result in another command?

I have a Windows .bat file which I would like to accept user input and then use the results of that input as part of the call to additional commands.
For example, I'd like to accept a process ID from the user, and then run jstack against that ID, putting the results of the jstack call into a file. However, when I try this, it doesn't work.
Here's my sample bat file contents:
#echo off
set /p id=Enter ID:
echo %id%
jstack > jstack.txt
and here's what shows up in jstack.txt:
Enter ID: Terminate batch job (Y/N)?
Try this:
#echo off
set /p "id=Enter ID: "
You can then use %id% as a parameter to another batch file like jstack %id%.
For example:
set /P id=Enter id:
jstack %id% > jstack.txt
The syntax is as such: set /p variable=[string]
Check out http://commandwindows.com/batch.htm or http://www.robvanderwoude.com/userinput.php for a more deep dive into user input with the different versions of Windows OS batch files.
Once you have set your variable, you can then go about using it in the following fashion.
#echo off
set /p UserInputPath=What Directory would you like?
cd C:\%UserInputPath%
note the %VariableName% syntax
set /p choice= "Please Select one of the above options :"
echo '%choice%'
The space after = is very important.
I am not sure if this is the case for all versions of Windows, however on the XP machine I have, I need to use the following:
set /p Var1="Prompt String"
Without the prompt string in quotes, I get various results depending on the text.
#echo off
set /p input="Write something, it will be used in the command "echo""
echo %input%
pause
if i get what you want, this works fine. you can use %input% in other commands too.
#echo off
echo Write something, it will be used in the command "echo"
set /p input=""
cls
echo %input%
pause
There is no documented /prompt parameter for SETX as there is for SET.
If you need to prompt for an environment variable that will survive reboots, you can use SETX to store it.
A variable created by SETX won't be usable until you restart the command prompt. Neatly, however, you can SETX a variable that has already been SET, even if it has the same name.
This works for me in Windows 8.1 Pro:
set /p UserInfo= "What is your name? "
setx UserInfo "%UserInfo%"
(The quotation marks around the existing variable are necessary.)
This procedure allows you to use the temporary SET-created variable during the current session and will allow you to reuse the SETX-created variable upon reboot of the computer or restart of the CMD prompt.
(Edited to format code paragraphs properly.)
#echo off
:start
set /p var1="Enter first number: "
pause
You can try also with userInput.bat which uses the html input element.
This will assign the input to the value jstackId:
call userInput.bat jstackId
echo %jstackId%
This will just print the input value which eventually you can capture with FOR /F :
call userInput.bat
There are two possibilities.
You forgot to put the %id% in the jstack call.
jstack %id% > jstack.txt
So the whole correct batch file should be:
#echo off
set /p id=Enter ID:
echo %id%
jstack %id% > jstack.txt
And/Or 2. You did put it in the code (and forgot to tell us in the question) but when you ran the batch file you hit the Enter key instead of typing an ID (say 1234).
What's happening is the result of these two mistakes:
jstack is supposed to be called with the id that you supply it.
But in your case (according to the code you supplied in the question) you called it without any variable. You wrote:
jstack > jstack.txt
So when you run jstack with no variable it outputs the following:
Terminate batch file Y/N?
Your second mistake is that you pressed Enter instead of giving a value when the program asked you: Enter ID:. If you would have put in an ID at this point, say 1234, the %id% variable would become that value, in our case 1234. But you did NOT supply a value and instead pressed Enter. When you don't give the variable any value, and if that variable was not set to anything else before, then the variable %id% is set to the prompt of the set command!! So now %id% is set to Enter ID: which was echoed on your screen as requested in the batch file BEFORE you called the jstack.
But I suspect you DID have the jstack %id% > jstack.txt in your batch file code with the %id (and omitted it by mistake from the question), and that you hit enter without typing in an id. The batch program then echoed the id, which is now "Enter ID:", and then ran jstack Enter ID: > jstack.txt
Jstack itself echoed the input, encountered a mistake and asked to terminate.
And all this was written into the jstack.txt file.
I have a little cmd I use when preparing pc to clients: it calls the user for input, and the rename the pc to that.
#ECHO "remember to run this as admin."
#ECHO OFF
SET /P _inputname= Please enter an computername:
#ECHO Du intastede "%_inputname%"
#ECHO "The pc will restart after this"
pause
#ECHO OFF
wmic computersystem where name="%COMPUTERNAME%" call rename name="%_inputname%"
shutdown -r -f
Dollar signs around the variable do not work on my Vista machine, but percent signs do.
Also note that a trailing space on the "set" line will show up between the prompt and user input.
Just added the
set /p NetworkLocation= Enter name for network?
echo %NetworkLocation% >> netlist.txt
sequence to my netsh batch job. It now shows me the location I respond as the point for that sample. I continuously >> the output file so I know now "home", "work", "Starbucks", etc. Looking for clear air, I can eavulate the lowest use channels and whether there are 5 or just all 2.4 MHz WLANs around.
Just to keep a default value of the variable. Press Enter to use default from the recent run of your .bat:
#echo off
set /p Var1=<Var1.txt
set /p Var1="Enter new value ("%Var1%") "
echo %Var1%> Var1.txt
rem YourApp %Var1%
In the first run just ignore the message about lack of file with the initial value of the variable (or do create the Var1.txt manually).
One other way which might be interesting. You can call a powershell script from where you can do pretty much anything, and send the data bach to cmd or do other stuff with something like this.
set var=myvar;
call "c:\input2.cmd" %var%.
Its kind of more flexible (You can send the data the same way with powershell).
So in your cmd, write this considering the ps script is in C::
PowerShell.exe -ExecutionPolicy unrestricted -Command "& {. C:\Input.ps1}"
And in your input.ps1 script, write this:
$var = Read-Host -Prompt "Your input"
Write-Host "Your var:",$var
#Do stuff with your variable

Resources