我正在制作一个脚本,如果计算机启动,主机名必须更改为特定名称。该名称位于 test.txt 文件中。
目前,我的文档中有一个测试.txt文件。
如果我这样做:
setlocal
set /p hostname=< test.txt
echo %hostname%
SBB-TEST
所以我可以将.txt文件中的文本放入变量中。但是如果我尝试:
WMIC computersystem where name="%computername%" rename name =%hostname%
我收到错误:无效的动词开关
这是我的第一个问题。
我的第二个问题是,如果我直接从批处理文件执行此操作。我在屏幕上收到以下错误:
set /p hostname= 0<test.txt
The system cannot find the file specified
为什么他将设置/p 主机名=未指定 0。
在rename
上使用 CALL 。您仍需要重新启动系统才能使新主机名生效。
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/
此外,HOSTNAME
已经是UNIX/Linux/Cygwin下的环境变量。我会称这个为别的东西。也许NEW_HOSTNAME
.