Reputation: 17
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!
Upvotes: 0
Views: 970
Reputation: 38613
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
Upvotes: 1