I'm trying to write a batch script that will do a RunAs for any given windows service (using explorer.exe to test) for any given user that can be input at time of running. What I currently have is this.
set /p Var1="Domain = "
set /p Var2="Username = "
set /p Var3="Service to open= "
RunAs /user:%Var1%\%Var2% "%Var3%" /seperate
On command line, this seems to work ok (with coded values instead of variables) but in a batch file it just seems to repeat itself, without opening what it's been specified (it doesn't even prompt for a password, so I can only presume its not even trying). Any idea why it's looping and what I can do to stop it?
Cheers
Hi #Gary Barnett i created something similar to this awhile back.
Here is an example;
#ECHO OFF
set /P Domainn=Enter Domain Name:
set /P Usern=Enter Username:
set userunas=runas /user:%domainn%\%usern% "
:optionmenu
CLS
ECHO 1 - Control.exe
ECHO 2 - MSinfo32.exe
ECHO 3 - Explorer.exe
ECHO q - Quit
ECHO.
set /P optionnum=Enter command number:
GOTO option%optionnum%
:option1
%userunas%control.exe
goto optionmenu
:option2
%userunas% %commonprogramfiles%\micros~1\MSInfo\msinfo32.exe"
goto optionmenu
:option3
%userunas%explorer.exe
goto optionmenu
:optionq
EXIT
Note: You must run the batch file as Administrator (right-click -> Run as Administrator) or it wont work.
/seperate is not a switch from RUNAS. So,
RUNAS /USER:"%Var1%\%Var2%" "%Var3%"
should go fine. If /seperate is a parameter to %Var3% program, use:
RUNAS /USER:"%Var1%\%Var2%" "%Var3% /seperate"
You may also add PAUSE at end of script so you can check results, while "debugging".
Related
Hi i wanted to change the value of hostname for new computer but things didn't work as i try to input the value and getting the value for the hostname. I try to restart the computer the result is still the same.. I am testing to see whether this script is capable of updating the hostname for pcs or computer name
thanks
here is my code
REM This script runs in MS-DOS Batch File Language
#echo off
set /p id= Enter ID or Hostname:
echo %id%
WMIC computersystem where caption='%ComputerName%' rename %id%
REM exit the applications
echo "Export completed successfully. Press any key to exit"
pause >nul
exit /B
Here's how I'd probably do it:
#Echo Off
Echo Your current name is %ComputerName%
:AskID
Set "ID="
Set /P "ID=Enter your new name: "
If Not Defined ID (Echo Can not be empty
GoTo AskID)
If /I "%ID%"=="%ComputerName%" Exit /B
If "%ID:~,1%"=="." (Echo Must not begin with a period
GoTo AskID)
Rem Put here some more checks for disallowed words or characters
WMIC ComputerSystem Where Name="%ComputerName%" Call Rename "%ID%"
Notes
This will need to be run 'As administrator'.
The change will not take effect until the next reboot.
It is important that you don't allow your end user, especially when running an administrative task to just enter anything at the prompt. Please consider following the Remarked line's advice.
I tested the below batch script to change the windows 10 hostname through batch script and it work perfectly fine try it out and reboot the system atlast !
set /p NEW_NAME="Please enter computer name: "
for /f %%i in ('hostname') do set OLD_NAME=%%i
echo %OLD_NAME%
echo %NEW_NAME%
WMIC computersystem where caption="%OLD_NAME%" rename "%NEW_NAME%"
pause
My batch script does the following:
user types in username and is added to a variable
trick to ask for password (hides input from user) and adds to variable
checks username and password authentication for domain GROUP using "net user" command
If user is found in set group, continue to map drive.
If user is not part of group restart at beginning
This script works when the username is found.
This script works when a username is found in a group
The problem is if the username is NOT FOUND.
When the user is NOT FOUND, it reports the "More help is available by typing NET HELPMSG 2221."
It just sits there and does not continue to prompt or anything.
I echoed the errorlevel and it comes out to 0, and reports the 0. However it still just sits there never reaching command prompt or going where the GOTO tells it.
I have put in errorlevels and I am unsure why it is stuck after the error message and does not continue. It is as if the batch script is not releasing from somewhere.
#echo off
:Question
Echo.
Echo.
SET /P HelperName=Enter Witness' Name:
Echo.
If %HelperName% EQU %Username% GOTO SameUserName
cls
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>%temp%\ftp.com
set /p password=What is your password? <nul
for /f "tokens=*" %%i in ('%temp%\ftp.com') do set "password=%%i"
del %temp%\ftp.com
cls
set i=0
set group=WGD
set user=%HelperName%
echo Checking if %user% is member of %group%...
for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1
if %i% gtr 0 (goto :member)
:nomember
echo %user% is not member of %group% Please Try Again
goto :question
:member
net use L: \\10.10.10.10\foldernamehere\TEMP /user:wgd\%helpername% %password%
if [%errorlevel%]==[0] goto deletedrive
goto error
:deletedrive
net use /delete L:
goto start
It is as if the script in still in another function.
Well it seems like I was not waiting long enough for the message to finish. It hangs for a bit, then continues on through the script. The wait however for the help message to clear is a little long, if you know how to clear faster please let me know.
So I am experimenting and trying to get better at using batch but I'm stuck on an issue, and I cant figure it out. The save/load allows the user to input a password and save it to the blah.blah, then later load it using the password. The problem is I want the program to recognize if the user has a password and if they dont, it will give them the option to exit or make a new password.
:passwordwant
cls
set /p "passwant=Enter your new password: "
(
echo %passwant%
)>save.sav
goto :passwordload
pause >nul
:passwordload
cls
echo Enter password to enter the programme
set /p "pass=>"
if %pass%== %passwant% goto :home
<save.sav(
set /p pass=
)
You can use if exist to determine if a file exists (or if not exist to determine if it doesn't).
if not exist save.sav (
echo Password not found
REM If you're using Windows XP or earlier, you need to use set /p instead of choice
choice /c:NQ /M "Press N to create a password. Press Q to quit."
if %errorlevel% equ 0 goto :passwordwant
if %errorlevel% equ 1 exit /b
)
I have a simple batch file:
ECHO OFF
CLS
ECHO WELCOME
ECHO.
ECHO Launching your app...
What I would like is to return to a default CMD state where the user can type commands, I mean, this state: C:[path][path].....>
I want to give to the user the possibility to continue to type anything he wants after the echo "Launching your app..."
So visually it would give this:
WELCOME
Launching your app...
C:[path][path][path]> _
#echo off
rem check if started from own process (use our own parameter)
rem and if not, spawn a new cmd with correct parameters and
rem keep it open
if not "%~1"=="__startcmd__" (
"%comspec%" /k "%~f0" __startcmd__ %*
exit
)
rem eliminate custom parameter of parameter list
shift
rem your custom screen
cls
title "command window"
prompt $p$g
echo WELCOME
echo Launching your app ...
Okay so heres what I'm trying to do. I'm trying to make a batch file with a user input for a login. I would like to have the ability to create a new account. I have an idea, I just don't know how to go around implementing it. I have the user input his desired username and password and have it save to a text document like so:
#echo off
set /p user=Enter your desired username:
set /p pass=Enter your desired password:
echo %user% >> log.txt
echo %pass% >> log.txt
That works fine for me, but now what I would like to do is call up those two lines so that when they entered the correct username and password it will take them to their menu.
I know for a fact the the call won't work well with this. Is there any way to do what I'm trying to do?
P.S. I am aware that the txt file is not secure. I have ways around that.
You may do this to recover the saved values:
(
set /P savedUser=
set /P savedPass=
) < log.txt
This way:
if "%user%" == "%savedUser%" if "%pass%" == "%savedPass%" goto accessGranted
Another way to save the values is this:
echo set savedUser=%user%> log.bat
echo set savedPass=%pass%>> log.bat
and to recover the saved values:
call log
I'm decently proud of this :) This is pretty much a complete overhaul of your method.
This has two different files: login.bat which handles the login and login2.bat which handles the registration.
login.bat:
#echo off
choice /c:RL /m "Choose an option: Register (R) or Login (L).:
if errorlevel 2 goto login
if errorlevel 1 goto register
:register
start /wait C:\[path]\login2.bat
cls
goto login
:in
cls
echo Welcome %u%
echo.
echo Bla Bla Bla or start "a program"
pause
exit
:login
set /p u=Username
set /p p=Password
and login2.bat:
#echo off
:a
set /p a="Choose a Username"
set /p b="Choose a Password"
echo.
choice /m "Are you sure you would like your Username to be %a% and your Password to be %b%?"
if errorlevel 2 goto a
set q="if %%u%% equ %a% if %%p%% equ %b% goto in"
for /f "tokens=*" %%I in (%q%) do set m=%%I
echo %m% >>C:\[path]\login.bat
exit
The reason why :in, the label which handles what happens after you login successfully has to be in the middle instead of at the end of login.bat is so that the username/password combinations could be appended to the :login label. Obviously you could add other embellishments and change the name of the files, but this is my basic design.
Hope this helps!
This is what I do
#echo off
:: some code here
set /p username=please create a username:
set /p password=please create a password:
echo %username%>username.txt
echo %password%>password.txt
This will create a text file for both your username and password
I hope this helped. :)