范围如何在.cmd文件中运行?

时间:2018-04-17 14:28:32

标签: batch-file command

我运行了一个.cmd文件脚本来检查用户是否在AD组中。

这个过程是这样的:

  1. 更新其AD GROUP列表
  2. 检查他们是否有正确的群组来安装应用
  3. 如果函数的结果是REM ======== SET variables ======== SET @ADGroups=C:\APPS\%USERNAME%\ADGroups.txt REM ======== update AD groups ======== gpresult /r > %@ADGroups% REM ======== APP1 ========= call:Check_ADGroup "App1 Name" IF %@InGroupYN% == Y ( call:Install_App ) REM ======== APP2 ========= call:Check_ADGroup "App2 Name" IF %@InGroupYN% == Y ( call:Install_App ) ,则跳到下一步检查
  4. 这是我的剧本:

    ::--------------------------------------------------------
    ::-- Function Check In AD GROUP
    ::--------------------------------------------------------
    :Check_ADGroup
    
    SET @Result=[]
    SET @InGroupYN=N
    
    FOR /F "delims=" %%a in ('FINDSTR /rc:"%~1" %@ADGroups%') DO SET @Result=%%a
    
    
    IF @Result == [] (
        REM ======== Set to No ======== 
        SET @InGroupYN=N
    
    ) ELSE (
        REM ======== Set to Yes ======== 
        SET @InGroupYN=Y
    )
    goto:eof
    
    @InGroupYN

    由于某种原因Y始终以<a href="link">John Doe</a> 运行,无论该函数是返回true还是false

    是否需要在命令行中使用变量考虑某些范围?

1 个答案:

答案 0 :(得分:1)

不改变你的结构...

为什么不这样使用If Defined

Rem ======== SET variables ========
Set "@ADGroups=C:\APPS\%UserName%\ADGroups.txt"

Rem ========  update AD groups ========
GPResult /R > "%@ADGroups%"

Rem ======== APP1 =========
Call :Check_ADGroup "App1 Name"
If Defined @InGroupYN (
    Call :Install_App
)

Rem ======== APP2 =========
Call :Check_ADGroup "App2 Name"
If Defined @InGroupYN (
    Call :Install_App
)

然后删除不必要的For循环和If / Else,改为使用条件Set

Rem ------------------------------------------------------
Rem -- Function Check In AD GROUP
Rem ------------------------------------------------------
:Check_ADGroup

Rem ======== Undefine Variable ========
Set "@InGroupYN="

Rem ======= Set to Yes if match =======
FindStr /IRC:"%~1" "%@ADGroups%" >Nul && Set "@InGroupYN=Y"

GoTo :EOF