Nick
Nick

Reputation: 499

If Computer Name Contains

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?

Upvotes: 1

Views: 17560

Answers (5)

Carl Murphy
Carl Murphy

Reputation: 11

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

Upvotes: 0

bledd
bledd

Reputation: 1

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

Upvotes: 0

MAbraham1
MAbraham1

Reputation: 1768

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

Upvotes: 0

mojo
mojo

Reputation: 4132

@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

Upvotes: 1

npocmaka
npocmaka

Reputation: 57252

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

Upvotes: 6

Related Questions