Loops 循环不在批处理文件中循环
我有个问题,似乎看不出哪里出了问题 我有这样一个代码,它意味着在一个文本文件中循环,并查看行的最后一个字,以查看行开头的用户是否应该获得对其指定笔记本电脑的提升访问权限 但是,代码只运行一次,然后退出Loops 循环不在批处理文件中循环,loops,batch-file,for-loop,Loops,Batch File,For Loop,我有个问题,似乎看不出哪里出了问题 我有这样一个代码,它意味着在一个文本文件中循环,并查看行的最后一个字,以查看行开头的用户是否应该获得对其指定笔记本电脑的提升访问权限 但是,代码只运行一次,然后退出 @echo off cls echo. echo The following script will process the entire list of students, giving each echo student administration rights to the laptop
@echo off
cls
echo.
echo The following script will process the entire list of students, giving each
echo student administration rights to the laptop and set the laptop description
echo where necessary.
echo.
pause
cls
FOR /F "tokens=1-6 delims=," %%a IN (accesselevations.txt) DO (
set School1=%%a
set LaptopID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
If /i "%%f" EQU "Admin" goto Elevate2
If /i "%%f" EQU "NoAdmin" goto NoElevate2
:Elevate2
set Desc1=!School1! - !LaptopID! - !Model1! - !Serial1! - !User1!
echo.
echo Now creating local admin account for !user! on !LaptopID!
echo.
echo Description: !Desc1!
echo.
pause
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net localgroup Administrators "GBN\!User1!" /add
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net config server /srvcomment:"!Desc1!"
:NoElevate2
cls
Echo.
Echo User !user1! is not allowed Local Administrator rights on !LaptopID!.
pause
)
pause
:End
该文件中有机密数据,因此我无法发布它,不幸的是,它包含如下内容
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
……等等
我希望我已经提供了足够的信息,我是新来的
提前感谢您对这一问题的介绍
托比
setlocal enabledelayedexpansion
,否则您将无法对变量使用感叹号FOR
循环If/i“%%f”eq“Admin”转到电梯2
If/i“%%f”eq“NoAdmin”转到NoElevate2
CALL
命令,脚本将在提升操作完成后返回到FOR
循环的末尾,GOTO
命令无法执行此操作
- 将
放在退出/b
之前,否则脚本将不会返回到:NoElevate2
命令点调用
@echo off
setlocal enabledelayedexpansion
cls
echo.
echo The following script will process the entire list of students, giving each
echo student administration rights to the laptop and set the laptop description
echo where necessary.
echo.
pause
cls
FOR /F "tokens=1-6 delims=," %%a IN (accesselevations.txt) DO (
set School1=%%a
set LaptopID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
If /i "%%f" EQU "Admin" (CALL:Elevate2) else (CALL:NoElevate2))
:Elevate2
set Desc1=!School1! - !LaptopID! - !Model1! - !Serial1! - !User1!
echo.
echo Now creating local admin account for !user! on !LaptopID!
echo.
echo Description: !Desc1!
echo.
pause
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net localgroup Administrators "GBN\!User1!" /add
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net config server /srvcomment:"!Desc1!"
exit/b
:NoElevate2
cls
Echo.
Echo User !user1! is not allowed Local Administrator rights on !LaptopID!.
pause
exit/b
:End
@echo关闭
setlocal
cls
回响
echo下面的脚本将处理整个学生列表,并给出每个
回显笔记本电脑的学生管理权限并设置笔记本电脑说明
必要时进行回音。
回响
暂停
cls
对于(accesselevations.txt)中的/F“tokens=1-6 delims=,”%%a(
设置School1=%%a
设置LAPTOID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
如果/i“%%f”eq“Admin”调用:Elevate2
如果/i“%%f”等于“NoAdmin”调用:NoElevate2
)
后藤:EOF
:电梯2
set Desc1=%School1%-%LaptopID%-%Model1%-%Serial1%-%User1%
回响
echo正在为%LaptoId%上的%user1%创建本地管理员帐户
回响
回显说明:%1%
回响
ECHO psexec\\%LaptopID%-u GBN\%ocusr%-p%ocpw%-n 10-e net localgroup管理员“GBN\%User1%”/add
ECHO psexec\\%LaptopID%-u GBN\%ocusr%-p%ocpw%-n 10-e网络配置服务器/srvcomment:%Desc1%
后藤:eof
:NoElevate2
回响
不允许回显用户%user1%对%LaptopID%具有本地管理员权限。
后藤:EOF
注:
添加以确保环境不会被运行污染setlocal
- 设置
似乎没有多大意义,因为它没有被使用Access1
而不是CALL:routine
-在块内goto
不是一个好主意,因为一些goto
版本的工作方式似乎不同。运行内部子例程时需要冒号cmd
- 在内部子例程中,可以使用
,因为它们在自己的上下文中运行%var%
!瓦尔代码>语法仅在被
调用时有效(未在原始批处理中执行)setlocal enabledelayedexpansion
命令只是psexec
ed。需要删除前面的ECHO
以执行ECHO
psexec
- “Nodamin”不应该应用“删除特权”例程吗
!用户代码>在
中更改为:elevate2
,因为%user1%
是由循环建立的,而不是user1
user
School1,Workstation1ID,Laptop1Model,Laptop1Serial,Student1Username,Admin
School2,Workstation2ID,Laptop2Model,Laptop2Serial,Student2Username,NoAdmin
School3,Workstation3ID,Laptop3Model,Laptop3Serial,Student3Username,Admin
School4,Workstation4ID,Laptop4Model,Laptop4Serial,Student4Username,NoAdmin
不难构建-可以通过替换姓名和其他敏感信息来轻松创建更漂亮的版本。Hey@Rafael我在明天上班之前无法测试这个,但仔细看,现在一切都有了意义,我想你很可能是一个传奇人物!我将随时通知您。:-)
School1,Workstation1ID,Laptop1Model,Laptop1Serial,Student1Username,Admin
School2,Workstation2ID,Laptop2Model,Laptop2Serial,Student2Username,NoAdmin
School3,Workstation3ID,Laptop3Model,Laptop3Serial,Student3Username,Admin
School4,Workstation4ID,Laptop4Model,Laptop4Serial,Student4Username,NoAdmin