DVLanleyC
DVLanleyC

Reputation: 153

change hostname from text in .txt file

I am making a script that if the computer starts up, the hostname has to change to a specific name. That name is in a test.txt file.

Currently I have a test.txt file in my documents.

If I do :

setlocal
set /p hostname=< test.txt
echo %hostname% 
SBB-TEST

So I can have the text from the .txt file into a variable. But If I try :

WMIC computersystem where name="%computername%" rename name =%hostname%

I get the error : Invalid Verb Switch

That is my first question.

My second question is, If I do this directly from a batch file.. I get following error on my screen :

set /p hostname= 0<test.txt
The system cannot find the file specified

Why does he change the set /p hostname=< test.txt to set /p hostname= 0

The 0 is nowhere specified.

Upvotes: 0

Views: 794

Answers (1)

lit
lit

Reputation: 16226

Use a CALL on the rename. You will still need to reboot the system for the new hostname to take effect.

WMIC computersystem where name="%computername%" CALL rename name =%hostname%

https://blogs.technet.microsoft.com/askds/2009/02/19/renaming-a-computer-using-wmic-and-how-to-get-around-that-aggravating-invalid-global-switch-error/

Also, HOSTNAME is already an environment variable under UNIX/Linux/Cygwin. I would call this one something else. Perhaps NEW_HOSTNAME.

Upvotes: 1

Related Questions