Changing Hostname of PC through Batch Script - batch-file

Lately I've started working on automating some of my tasks. Since I work with windows machines mostly I decided to learn batch and powershell.
For now I have faced issue with variables in CMD.
The task is extremely simple. I want this script to change the hostname of the computer. The new name should be user input. I want it to find default hostname (whatever it is) and then change it to what I tell it to.
At this moment I was using command:
hostname
set /p future_name="Input what you want Hostname to be: "
wmic computersystem where caption='%computername%' rename %future_name%
shutdown /r /t 5
The tutorials that I keep finding say that the hostname of PC should already be written in the code. However, this script is being used on multiple machines which all bear different names.
I understand that this is a newbie question but I got stuck here.
C:\Windows\system32>wmic computersystem where caption='%computername%' rename 'future_name'
Executing (\\DESKTOP-SMTHSMTH\ROOT\CIMV2:Win32_ComputerSystem.Name="DESKTOP-SMTHSMTH")->rename()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 87;
};
Press any key to continue...
P.S.
I tried running the script with another variable which will be me just retyping the hostname displayed above but it doesn't work still.
hostname
set /p my_name_is="Please retype my current name provided above: "
set /p future_name="Input what you want Hostname to be: "
wmic computersystem where caption='%my_name_is%' rename %future_name%
:: shutdown /r /t 5
PAUSE
It also returns the same as before. Any help will be appreciated!

Using the Where clause methodology you've shown in your question code, as far as I'm aware, is flawed. You cannot use the Create verb with the Where clause.
You should therefore, given a valid name and length, use the Call verb with the Rename method instead.
#Set "FutureName=%COMPUTERNAME%"
#Set /P "FutureName=Input what you want your new name to be>"
#"%__AppDir__%wbem\WMIC.exe" ComputerSystem Where "Name='%COMPUTERNAME%'" Call Rename "%FutureName%"
#"%__AppDir__%wbem\WMIC.exe" OS Call Reboot

Related

bat file to prompt for user to input computer name

As I have to install multiple computers.....is there any way to use:
WMIC ComputerSystem where Name=COMPUTERNAME call Rename Name=NewName
but instead of "NewName" to prompt an input name....
the reason I ask, is I don't want to give same computer name to all computers, and I dont want to edit the bat file for each computer.
I'm also using:
net user username password /ADD
for this one, can I also get a prompt to insert desired user and password so I don't need to give same username and password or to edit the bat file
is there any what to automate this with users's input interaction?
Since it appears you want an automated way to change your PC name, this script should help you. Keep in mind you will have to reboot your PC for the effects to take place.
For the computer name, we can automate that using a dirty method of %computername%
#ECHO OFF
SET NEWNAME=
SET /P NEWNAME= Select a new PC name:
::Change PC Name
WMIC ComputerSystem where Name='%computername%' call Rename Name=%NEWNAME%
CLS
Echo Process Complete..
Pause.
Keep in mind that you can edit a remote PC on the same network using the script bellow. You can throw this in a batch file and change multiple PC's at once.
WMIC /node:"Jon-Laptop" /user:Admin /password:password123 computersystem call rename "Jon-Tech"

NET USE multiple servers copy files

I want to copy specific file from one machine to 5 other machines, so I have server list file contains the ip addresses of each machine like:
10.10.1.3
10.10.1.4
10.10.1.5
10.10.1.6
10.10.1.7
in my batch file:
SET File=C:\Files\servers.txt
SET User=user
SET Password=pass
IF EXIST b:\ (
NET USE b: /DELETE /Y
)
FOR /F %%A IN (%File%) DO (
START /WAIT NET USE b: \\%%A\C$\Temp /user:%User% %Password%
COPY C:\Logs\L1.log b:\L1.log /Y
IF EXIST b:\ (
NET USE b: /DELETE /Y
)
)
the problem is in the first server I get error message The system cannot find the drive specified but for the others servers everything works great.
I think it's something with the NET USE of course maybe the map network is deleted before finish copy?
Is there any way in a batch file to loop some servers and for each one of the servers open map network copy files wait till copy is completed and move on to next server?
EDIT:
I have an update for this problem:
the source machine and the target machine are both in different domains.
I have a user define as admin in both of the machines.
The machines knows each other (I can open the target folder in the source machine like \server\C$\temp and I can paste anything I want there)
I tried to copy files without using net use and just copy from C:\file.log \server\c$\temp\file.log for each server (I have 5) and for 3 servers it worked and the other two I had an error: Logon failure: unknown user name or bad password
FOR /F %%A IN (%File%) DO (
COPY C:\temp\file.log \\server\c$\temp\file.log /Y
)
What can be the problem?
Please help?
Thank you in advanced.
The best way would be if the user/password is the same on every machine (credientals shared throughout the domain)
In that case, no need for NET USE, just copy to UNC path directly
FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\C$\Temp\L1.log
I understood you have a problem on 2 machines out of 5. You cannot access C$ directly if you are not administrator of the machines.
Check:
domain (echo %USERDOMAIN% on the remote machine): All machines must be on the same domain for it to work
whether you are administrator or not on the remote machine: you cannot mount drives with the C$ trick if you are not administrator.
There is an alternate solution for you if this doesn't work out:
Share C:\TEMP (read/write) as the same name ex: sharedtemp on the 5 machines (or ask an admin to do that)
Then adapt the script like this:
FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\sharedtemp\L1.log
Even if you are not an administrator, you'll be able to access the share that way.
When using START command, you need to either specify new window name, or leave it blank in double quotes, if you want it to run in the same open CMD window. However, you don't need to use START in this particular case. Try adding timeout for the 1st or all server connections in the list:
IF EXIST b:\null NET USE b: /DELETE
:: more code here
NET USE b: \\%%A\C$\Temp /user:%User% %Password%
if "%%A"=="10.10.1.3" timeout 5 >nul

Using dash and switches in batch script

I just created simple batch script. I want to run uninstall.exe with switches like "-q" "-splash Uninstall"
Here is the code;
#echo off
echo This script will uninstall some features.
pause
SET path=C:\Program Files\Program\uninstall.exe -q -splash Uninstall
START "" "%path%"
pause
If I run this code it gives an error:
Windows cannot find 'C:\Program Files\Program\uninstall.exe -q -splash Uninstall'
Make sure you typed the name correctly, and then try again.
If I remove switches, uninstall process starts normally.
So how can I use this swtiches in a batch file?
As an aside, don't use path as an arbitrary choice of variable name. It has a special significance in Windows (and Unix-derived systems too).
Your main problem is that you are including the switches in your quoted string, which is then treated as a whole as the executable filename. Put your quotes only around the filename, and leave the switches outside:
SET command="C:\Program Files\Program\uninstall.exe" -q -splash Uninstall
START "" %command%
(The only reason for the quotes is the fact that the pathname contains spaces.)
Also, you don't really need to use a variable at all, but I've used one since you used one.
I'm not quite sure if every program you come across will have a uninstall.exe file waiting for you in the C:\Program Files(place program name here)\ directory. Even if it does, you will probably have to control it from the GUI. However, looking at another stack overflow thread here, I would like to credit the users Bali C. and PA. for coming up with a possible solution to uninstall files using a batch file by using the registry key to find an uninstall file for windows programs. I will re-paste PA.'s code below:
#echo off
for /f "tokens=*" %%a in ('reg query hklm\software\Microsoft\Windows\CurrentVersion\Uninstall\ ^| find /I "%*"') do (
for /f "tokens=1,2,*" %%b in ('reg query "%%a" /v UninstallString ^| find /I "UninstallString"') do (
if /i %%b==UninstallString (
echo %%d
)
)
)
This code will find the uninstall file for a specific program from the registry, and then it will print out the command needed to run the uninstall file. Remove the 'echo' to just run these commands when you are sure they are correct. However, even this will probably require using the program's uninstall GUI. I don't think this would be terribly inefficient. Is there any other specific reason you want to use a batch file besides efficiency?
I hope this helps!

Batch-file - Fetch remote computer variable

I've got a VERY big pre-existing batch file that I need to add some logic to run the correct EXE depending on whether it is a x86 or x64 bit machine. I can't use PowerShell because for some reason they have it pseudo disabled on most of these machines.
What I'm trying to do is use PSEXEC to fetch the system variable PROCESSOR_ARCHITECTURE from the remote, then create a local system variable from the output.
If I run this command from a Win10x64 CMD prompt:
C:\PsTools>c:\pstools\PSEXEC.exe \\TEST-Win7x86 cmd /c echo ^%PROCESSOR_ARCHITECTURE^%
I get the correct remote x86 value, but can't do anything with it:
x86
cmd exited on TEST-Win7x86 with error code 0
.
I tried testing this method to get the output into a variable:
FOR /F "delims=" %i IN ('c:\pstools\PSEXEC.exe \\TEST-Win7x86 cmd /c echo ^%PROCESSOR_ARCHITECTURE^%') DO set proc=%i
I'm getting back the local AMD64 value:
cmd exited on TEST-Win7x86 with error code 0.
C:\PsTools>set proc=AMD64
.
Anyone spot what I'm doing wrong?
Thanks!
-Matt
After messing with this most of the day, I think I have it figured out shortly after submitting this question. I had to double-up the carrots for some reason. I'm not exactly sure why I thought to try it or why it works, but I'm getting the correct output now.
C:\PsTools>FOR /F %i IN ('c:\pstools\PSEXEC.exe \\TEST-Win7x86 cmd /c echo ^^%PROCESSOR_ARCHITECTURE^^%') DO set proc=%i
Output:
cmd exited on TEST-KW-O3040MD with error code 0.
C:\PsTools>set proc=x86
.
And as aschipfl corrected below, this is the correct context for in the batch script. Thanks aschipfl!
FOR /F %%i IN ('"\\Server\Shared Folder\PSEXEC.exe" \\TEST-Win7x86 cmd /c echo ^^%%PROCESSOR_ARCHITECTURE^^%%') DO set PROC=%%i

CMD: Save date and time as a file name

I wrote script to remove temporary files.
Script creates log with all files removed:
SET RemoveLog=RemoveLog_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%__%time:~0,2%_%time:~3,2%.txt (that will create file like RemoveLog_2014_03_09__09_12.txt)
and to save something in this log I use:
ECHO. >> %MEMLET%\%RemoveLog% (Empty line in this case)
This have been working fine so far but on one laptop something else is happening.
File is saved as RemoveLog_2014_03_09__ (09_12.txt is missing)
And everywhere I used >> %MEMLET%\%RemoveLog% in the script, In the log at the end of every line i see 9_12.txt
Why is this happening?
You don't get what you expected because %DATE% returns the current date using the windows settings for the "short date format". This setting is fully (endlessly) customizable.
One user may configure its system to show the short date as Wed030914; while another user (even in the same system) may choose 09/03/2014. It's a complete nightmare for a BAT programmer.
One possible solution is to use WMIC, instead. WMIC is the WMI command line interface to WMI. WMI Windows Management Instrumentation is the http://en.wikipedia.org/wiki/Windows_Management_Instrumentation
WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table
returns the date in a convenient way to directly parse it with a FOR.
Completing the parse and putting the pieces together
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year ^| findstr /r /v "^$"') DO (
SET REMOVELOG=RemoveLog_%%F_%%D_%%A_%%B_%%C.txt
)

Resources