我正在尝试创建多个用户,并使用带有批处理脚本的for循环将其添加到本地管理员组,但不会在哪里。我使用以下代码:
@echo off
SET userpassword=Test@123
SET user1=bob
SET user2=peter
SET user3=john
SET user4=jack
FOR /L %%i IN (1,1,4) DO (
net %user%%i% %userpassword% /logonpasswordchg:yes /add
net localgroup Administrators %user%%i% /add
)
pause
答案 0 :(得分:3)
您需要启用延迟扩展。 net命令使用错误,您应该尝试在自动化之前成功创建帐户。 (那里也有一个拼写错误%userpassowrd%)我认为这就是你所需要的:
@echo off
setlocal enabledelayedexpansion
set userpassword=Test@123
set user1=bob
set user2=peter
set user3=john
set user4=jack
for /l %%i in (1,1,4) do (
net user !user%%i! %userpassword% /logonpasswordchg:yes /add
net localgroup Administrators !user%%i! /add
)
pause
答案 1 :(得分:1)
@ECHO OFF &SETLOCAL
SET userpassword=Test@123
SET user1=bob
SET user2=peter
SET user3=john
SET user4=jack
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%i IN (1,1,4) DO (
net !user%%i! %userpassword% /logonpasswordchg:yes /add
net localgroup Administrators !user%%i! /add
)