If Computer Name Contains

Nick picture Nick · Jun 25, 2013 · Viewed 13.3k times · Source

I need to create a batch file that moves computer name object in Active Directory only if computer name contains some string, like:

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" )

What is the correct command please?

Answer

npocmaka picture npocmaka · Jun 25, 2013
set check_computername=%computername:LAP=%
if "%check_computername%" EQU "%computername%" (
  echo computer name contains "LAP"
) else (
  echo computer name does not contain "LAP"
)

You can put your stuff in if and else blocks.

Case Insensitive solution:

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