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
Related
There are things that we prefer not to understand in order to have an easier life to live.
But this is not something I can choose...
I made a batch file (or macro.doskey) to get the charset code. And it worked perfectly for a long time...
Basically it runs chcp:
> chcp
Code page active: 850
and then wraps the return before and after the colon
assigning what comes after to a variable:
FOR /F "tokens=1,* delims=:" %%s in ('CHCP') do (
#ECHO %%t
IF NOT "%1" == "" (SET %1=%%t)
)
For example:
> getCHCP.bat myVar
850
> ECHO %myVar%
850
However it started to lock, waiting for ENTER or displaying several echo messages. For example:
> getchcp myVar
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
850
I started to mix until I decided to change the ECHO %%t to ECHO %%s, and guess what?
No, is that the Bill Gates skull? Is it an easter egg from Microsoft? A virus?
No, none of that, this is just my autorun's welcome message.
This can be configured in
<[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]autorun>
In my case I called a batch file which, among other things, gives several echos showing this skull on the screen.
But the question is, why does it act like it reloads the autorun in background
when I've already opened the command prompt?
And why does it leave
everything in the buffer so that %%s pulls it again to the (Page code active) ':'?
And why are
you giving lots of ECHO is off on %%t when the only thing after
Code page active: is a number?
And the most important: How I solve it?
It's obvious, you already point to the problem.
this is just my autorun's welcome message.
This can be configured in
<[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]autorun>
The line FOR /F %%s in ('CHCP') ... start CHCP but that will be done in a NEW child cmd.exe instance.
And a NEW cmd.exe instance runs the autorun command!
Just before it starts your chcp.
You can disable the autorun at all, or add some code to detect the difference between a new cmd.exe instance for the user against a new instance from a FOR /F.
Put this code at the start of your autorun batch file
#echo off
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the originial!!!
set "_ccl_=!cmdcmdline!"
REM *** %1 contains only data, when the script itself was called from the command line
if "%~1" NEQ "" (
goto :direct_call
)
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
REM ***** INTERACTIVE ****
REM *** Show your skull or something else
)
exit /b
I am trying to setup a batch script which can connect to a set of servers and execute start script. Since there is a password getting saved in the commands.txt file. I need to delete it after the execution of start on remote servers. But that del command gets executed before everything and which is causing issues for the loop and errors out that commands.txt file is missing. Not sure how thats getting executed before the loop when its put after the loop. How can I fix this?
Below is the code I am trying.
#echo off
Echo Please enter your password in the popup window and then press enter
set tempbat="%temp%\p.cmd"
REM Create temporary batch file to make popup window for entering password 'masked'
echo mode 20,1 >%tempbat%
echo color EF >>%tempbat%
echo Title Enter Password >>%tempbat%
echo setlocal enabledelayedexpansion >>%tempbat%
echo set /p Pass= >>%tempbat%
echo echo !pass!^>"%temp%\pass.txt" >>%tempbat%
echo exit >>%tempbat%
echo exit >>%tempbat%
start /wait "" %tempbat%
set /p Password=<"%temp%\pass.txt"
#echo echo %password% ^| sudo -S -u x0ats echo startup.sh start>>
%cd%\commands.txt
#echo read>> %cd%\commands.txt
for /f "delims=" %%a in (%cd%\serverlist.txt) DO (
Start PuTTY username#%%a -pw %password% -m "%cd%\commands.txt"
)
del %cd%\commands.txt}
There are a few issues in your CMD script, but most specifically, as I mentioned above you are not putting double quotes around paths.
Additionally you were adding spaces to the ends of all of your line sin the bat file but that will cause you to collect the wrong PW, you also didn't delete your temp bat file or password file after the fact.
It seems like a bit of trouble to go through just to pop up a separate window, you could forgo creating a second batch file and call a sub function instead by making your script support cmd line arguments.
In Any case this should work more as you expect:
#ECHO OFF
ECHO.Please enter your password in the popup window and then press enter
SET "_TempBat=%temp%\p.cmd"
SET "_PWFile=%temp%\pass.txt"
SET "_CMDs=%cd%commands.txt"
REM Create temporary batch file to make popup window for entering password 'masked'
ECHO.SETLOCAL>"%_TempBat%"
REM ECHO.ECHO OFF >"%_TempBat%"
ECHO.mode CON COLS=42 LINES=1 >"%_TempBat%"
ECHO.color CF>>"%_TempBat%"
ECHO.Title Enter Password >>"%_TempBat%"
ECHO.SET /p "_Pass=Enter Password: " >>"%_TempBat%"
ECHO.ECHO.%%_pass%%^>"%_PWFile%">>"%_TempBat%"
ECHO.exit>>"%_TempBat%"
ECHO.exit>>"%_TempBat%"
START /wait "" "%_TempBat%"
DEL /F /Q "%_TempBat%"
IF NOT EXIST "%_PWFile%" (
"%_PWFile%" Not Created!
) ELSE (
FOR /F "Tokens=*" %%A IN ('TYPE "%_PWFile%"') DO (
SET "_PW=%%A"
DEL /F /Q "%_PWFile%"
)
)
#ECHO.ECHO.%_PW% ^| sudo -S -u x0ats ECHO.startup.sh start>> "%_CMDs%"
#ECHO.read>> "%_CMDs%"
for /f "delims=" %%a in ('Type "%_CMDs%"') DO (
Start PuTTY username#%%a -pw %_PW% -m "%_CMDs%"
)
Pause
DEL /F /Q "%_CMDs%"
ALSO I changed the colors you chose Bright Yellow on Bright right is hardly readable, I used Bright white on Bright Red. oh and I also added an Echo Off into your second script because again that was not helpful to have all the extra stuff in there
Also I looked at this and noticed that you were not going to get the PW saved in the PW file so I fixed that by using %% so that the first % will get stripped off.
I'm using an IF statement to check if a computer might be domain joined before attempting to change the name of the computer. I have 2 blocks of code.
This block never has an issue on any Windows 7 or newer. But offers no checks before renaming.
#echo off
set /P "UserInput=Enter New Computer Name: "
wmic computersystem where name="%computername%" call Rename name="%UserInput%"
pause
But this block of code always exits in Error
#echo off
IF \\%computername%==%logonserver% (
echo \\%computername% == %logonserver%
set /P "UserInput=Enter New Computer Name: "
pause
wmic computersystem where name="%computername%" call Rename name="%UserInput%"
pause
) ELSE (
echo This computer may be DomainJoined contact your System Administrator
pause
)
The echo after comparison in the IF was strictly so I could verify the output. It was intended for debugging and added for clarity. This is the actual code with nothing omitted.
The Error is
ERROR:
Description = Invalid method Parameter(s)
If I force the comparison to fail. The ELSE statement runs. I don't believe the error is in the IF/ELSE
In order to remove the need to delay variable expansion, which is your issue, you could remove the need to include that portion of the code inside an If block:
#Echo Off
If /I Not "\\%COMPUTERNAME%"=="%logonserver%" (
Echo This computer may be DomainJoined; contact your System Administrator
Pause
Exit /B
)
Set /P "UserInput=Enter a new name for this computer: "
Pause
WMIC ComputerSystem Where "Not Name='%UserInput%'" Call Rename "Name='%UserInput%'"
Pause
You could also consider removing the If block completely by including an additional Where filter:
#Echo Off
Set /P "UserInput=Enter a new name for this computer: "
Pause
WMIC ComputerSystem Where "Not Name='%UserInput%' And Not Name='%logonserver:~2%'" Call Rename "Name='%UserInput%'"
Pause
The end user is free to type nothing or anything they wish; I would strongly advise that you perform some sort of validation on that input before considering renaming the system. Most certainly with respect to allowed naming conventions, disallowed characters, and even rude or offensive strings.
Okay here's another one.
So the idea for this batch uses two loops, but I feel like I lack the proper knowledge but here we go:
Batch code:
(Please be advised, that when user's are assigned computers, they are assigned by the description. Not my choice but anyway, it searches for computers owned by the description.)
#echo off
echo First and Last Name of User:
echo.
set /p name=
set desc="assigned to %name%"
echo.
dsquery computer domainroot -desc %desc%*
for /f "delims=," %%a in ('dsquery computer domainroot -desc %desc%*' ) do set CompName=%%a123
set CompName=%CompName:~4,-3%
pause
for /f "delims=" %u in ('wmic /node:%CompName% COMPUTERSYSTEM GET USERNAME^|find "\"') do set userfound=%~nxu
pause
The batch seems to break at the second loop. I feel as though maybe because the first loop that is assigned a variable, it won't allow for a second for /f command to run properly.
Any suggestions?
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.