1

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?

Java D
  • 436
  • 1
  • 8
  • 18
Nick
  • 499
  • 4
  • 11
  • 25

5 Answers5

6
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
npocmaka
  • 55,367
  • 18
  • 148
  • 187
1
@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
mojo
  • 4,050
  • 17
  • 24
0

The logic has to be reversed. Here's a case-insensitive solution:

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%
)
MAbraham1
  • 1,717
  • 4
  • 28
  • 45
0

Came across this issue today, this is how I solved it..

Say you have different names for desktops, laptops etc ie (DESKTOP0001, LAPTOP0001) etc, then this method will work nicely.

What you want to do is grab the first few characters of the name, you can use :x,y within a variable to do this.

Example

echo %compuername:~0,6%

The output for this would be DESKTO (first 6 characters starting from position 0)

(echo %computername:~1,6% would give you ESKTOP )

Quick proof test..

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

So for my use, I used

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
bledd
  • 1
  • You solved the problem `string starts with`, but the question was about `string contains` – jeb Dec 05 '19 at 14:30
0

All the solutions work, but instead of static positioning, %computername:~0,6%, and instead of calling to run an external program "find /i", I would just use the string compare with string replacement method

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