【问题标题】:If Computer Name Contains如果计算机名称包含
【发布时间】:2013-06-21 22:26:12
【问题描述】:

我需要创建一个批处理文件,仅当计算机名称包含一些字符串时才在 Active Directory 中移动计算机名称对象,例如:

If %computername% contains "LAP" 
( dsmove "CN=%computername%,CN=Computers,DC=domain,DC=local" -newparent"OU=**Laptops**,OU=Computers,OU=Company,DC=domain,DC=local" )

    If %computername% contains "DESK" 
        (dsmove "CN=%computername%,CN=Computers,DC=domain,DC=local" -newparent "OU=**Desktops**,OU=Computers,OU=Company,DC=domain,DC=local" )

请问正确的命令是什么?

【问题讨论】:

    标签: if-statement batch-file contains computer-name


    【解决方案1】:

    所有解决方案都有效,但不是静态定位,%computername:~0,6%,也不是调用运行外部程序“find /i”,而是使用字符串比较和字符串替换方法

    If Not "%computername% == "%computername:LAP=%" (
       dsmove "CN=%computername%,CN=Computers,DC=domain,DC=local" -newparent"OU=**Laptops**,OU=Computers,OU=Company,DC=domain,DC=local"
    )
    
    If Not "%computername% == "%computername:DESK=%" (
       dsmove "CN=%computername%,CN=Computers,DC=domain,DC=local" -newparent "OU=**Desktops**,OU=Computers,OU=Company,DC=domain,DC=local"
    )

    【讨论】:

      【解决方案2】:

      今天遇到这个问题,我就是这样解决的..

      假设您对台式机、笔记本电脑等有不同的名称,即(DESKTOP0001、LAPTOP0001)等,那么这种方法会很好地工作。

      您要做的是获取名称的前几个字符,您可以在变量中使用 :x,y 来执行此操作。

      例子

      echo %compuername:~0,6%
      

      此输出将是 DESKTO(从位置 0 开始的前 6 个字符)

      (echo %computername:~1,6% 会给你 ESKTOP)

      快速验证测试..

      if %computername:~0,6% == DESKTO echo yes-Desktop
      

      所以为了我的使用,我使用了

      if %computername:~0,6% == DESKTO goto Desktop
      if %computername:~0,6% == LAPTOP goto Laptop
      
      goto end
      
      :Desktop
      enter Desktop commands here
      goto end
      
      :Laptop
      enter Laptop commands here
      goto end
      
      :end
      

      【讨论】:

      • 你解决了问题string starts with,但问题是关于string contains
      【解决方案3】:

      逻辑必须颠倒。这是一个不区分大小写的解决方案:

      setlocal enabledelayedexpansion    
      set nameSearch=Lap
      set checkComputerName=!computername:%nameSearch%=!
      if "%checkComputerName%" NEQ "%computername%" (
        echo %nameSearch% found in %computername%
      ) else (
        echo %nameSearch% not found in %computername%
      )
      

      【讨论】:

        【解决方案4】:
        @ECHO %COMPUTERNAME% | find /I "LAP"
        IF NOTERRORLEVEL 1 ( dsmove ... OU=laptop ... )
        GOTO :EOF
        @ECHO %COMPUTERNAME% | find /I "DESK"
        IF NOTERRORLEVEL 1 ( dsmove ... OU=desktop... )
        GOTO :EOF
        

        【讨论】:

          【解决方案5】:
          set check_computername=%computername:LAP=%
          if "%check_computername%" EQU "%computername%" (
            echo computer name contains "LAP"
          ) else (
            echo computer name does not contain "LAP"
          )
          

          你可以把你的东西放在ifelse 块中。

          不区分大小写的解决方案:

          echo %check_computername%| Find /I "LAP" >nul 2>&1 || echo does not contain LAP
          echo %check_computername%| Find /I "LAP" >nul 2>&1 && echo does not contain LAP
          

          【讨论】:

          • 存在逻辑错误:将 EQU 更改为 NEQ 或调整 ECHO
          • 没有。我的计算机名称中包含“LAP”。我试图运行这个脚本,它说计算机名称不包含“LAP”。
          • @npocmaka 解决方案应该有效,注意替换区分大小写!
          • 那么如何区分大小写呢?
          • 在比较之前将计算机名称转换为大写请参阅robvanderwoude.com/battech_convertcase.php
          猜你喜欢
          • 1970-01-01
          • 2016-03-03
          • 2016-02-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多