【问题标题】:Loop not looping in batch file循环不在批处理文件中循环
【发布时间】:2014-02-04 11:45:00
【问题描述】:

我有一个问题,似乎看不出哪里出错了。

我有这段代码,用于循环遍历一个文本文件,并查看该行的最后一个单词以查看该行开头的用户是否应该获得对其分配的笔记本电脑的提升访问权限。

但是代码只运行一次,然后退出。

@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

AccessElevations 文件中包含机密数据,因此很遗憾我无法发布它,但它包含类似这样的内容

School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot

...等等。

我希望我已经提供了足够的信息,我是新手。

提前感谢您对这个问题的任何启发。

托比

【问题讨论】:

    标签: loops batch-file for-loop


    【解决方案1】:
    • 使用setlocal enabledelayedexpansion,否则您将无法对变量使用感叹号。
    • Elevate2 之前关闭FOR 循环
    • 改变这个:

      If /i "%%f" EQU "Admin" goto Elevate2

      If /i "%%f" EQU "NoAdmin" goto NoElevate2

    收件人:

    If /i "%%f" EQU "Admin" (CALL:Elevate2) else (CALL:NoElevate2)
    

    使用CALL命令,你的脚本将在完成提升操作后返回到FOR循环的末尾,GOTO命令不会这样做。

    • exit/b 放在:NoElevate2 之前,否则您的脚本将无法返回到CALL 命令点。

    你的代码应该是这样的:

    @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
    

    【讨论】:

    • 嘿@Rafael 直到我明天开始工作,我才能测试这个,但仔细看看,现在一切都说得通了,我想你很可能是个传奇人物!我会随时通知你。 :-)
    【解决方案2】:
    @echo off
    setlocal
    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
    If /i "%%f" EQU "NoAdmin" CALL :NoElevate2
    )
    GOTO :EOF
    
    :Elevate2
    set Desc1=%School1% - %LaptopID% - %Model1% - %Serial1% - %User1%
    echo.
    echo Now creating local admin account for %user1% on %LaptopID%
    echo.
    echo Description: %Desc1%
    echo.
    ECHO psexec \\%LaptopID% -u GBN\%ocusr% -p %ocpw% -n 10 -e net localgroup Administrators "GBN\%User1%" /add
    ECHO psexec \\%LaptopID% -u GBN\%ocusr% -p %ocpw% -n 10 -e net config server /srvcomment:"%Desc1%"
    GOTO :eof
    
    :NoElevate2
    Echo.
    Echo User %user1% is not allowed Local Administrator rights on %LaptopID%.
    GOTO :EOF
    

    注意事项:

    • 添加setlocal以确保环境不会被运行污染

    • 设置Access1 似乎没有多大意义,因为它没有被使用。

    • CALL :routine 而不是goto - 在一个块中goto 不是一个好主意,因为某些cmd 版本的工作方式似乎不同。冒号是运行内部子程序所必需的

    • 在内部子例程中,可以使用%var%,因为它们在自己的上下文中运行。

    • !var! 语法仅在被setlocal enabledelayedexpansion 调用时有效(不在原始批处理中执行。)

    • psexec 命令只需 ECHOed。需要去掉前面的echo才能执行psexec

    • 不应该为“Nodamin”应用“删除权限”例程吗?

    • !user!: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
    

    构造起来并不难 - 可以通过替换名称和其他敏感信息来使用更漂亮的版本轻松完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 2023-03-23
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多