No announcement yet.

Moving Computer Account using a script

  • Filter
  • Time
  • Show
Clear All
new posts

  • Moving Computer Account using a script

    Is it possible to move a computers using vbscript /batch by tracing their IP.
    In my company we have a subnet per area.
    172.16.xx.xx Main Office
    10.10.xx.xx Remote Location 1
    192.168.xx.xx Main Office

    We have already know who are the current local administrators per pc (Thanks to MOD REMS !!!) .

    We need the following:
    1: Relocating Computer Accounts using vbscript/bat to their respective OU's by
    tracing their IP's
    computer1 (192.168.xx.xx) must be move to OU=Accounting
    computer2 (172.xx.xx.xx) must be move to OU=Marketing

    2: Script will skip the computer account that already been relocated
    3: Generate a report who is moved or not

    We will run this script on schedule basis.



  • #2
    Re: Moving Computer Account using a script

    Originally posted by totoy bato View Post
    Is it possible to move a computers using vbscript /batch by tracing their IP.
    We will run this script on schedule basis.
    Are "we" all members of the domain admins group?

    You can use a vbscript to determine the subnet of the computer, but you must connect to the computer first to retrieve the ipaddres and subnetmask first. Where can the script find the names of the computers that are not yet in right OU?
    (It is possible to search all ou's/containers in AD for computer objects, connect to each computer to retrieve the networkID (if the computer is online) then check if it is in the right OU already, If not move the object to the right container. But this is something you want to run in the evening, but then most clients won't be online)

    I think a better solution would be to use a logonscript (that you are using alreay) that is writing computer information to a logfile.
    Two options
    1. Add an extra field to the log containing the value of the environment variable %logonserver%. That is the DC closest to the client. Use a vbscript to read the logs of each client, and to get the networkID of the LogonServer, assuming the networkID of the client will be the same.
    2. Instead of just the current IPaddress of the client you could also add the SubnetMask to the log. The vbscript then can calculate the networkID from the two values.
    :: sample how to
    :: determine current IP address AND the SubnetMask
    :: of the computer by using a batch (IP version 4)
    @echo off
    setlocal enabledelayedexpansion
    :: Get IP address and SubnetMask (IP version 4)
    For /f "tokens=2 delims=[]" %%* in ('ping.exe %computername% -n 1 -w 1') Do set "strIP=%%*"
    For /f "delims=" %%* in ('ipconfig.exe') do (
       If "!def!"=="yes" (
         For /f "tokens=2 delims=:" %%* in ('echo.%%*') do (
             Set "def=%%*" & Set "SubnetMask=!def: =!"
         ) & GoTo:prefixlen )
       >nul (echo.%%* |find "%strIP%") &&(
         set "def=yes" )
    For /f "tokens=1,2,3,4 delims=." %%a in ('echo.%SubnetMask%') do (
      call:digit %%a %%b %%c %%d
      Goto :done2
    Goto:eof --------------------
    :digit    subroutine
     Set/a "prefixlen=0"
     Set "digit="
     If {%1}=={0} (set/a prefixlen=%prefixlen%+0)
     If {%1}=={128} (set/a prefixlen=%prefixlen%+1)
     If {%1}=={192} (set/a prefixlen=%prefixlen%+2)
     If {%1}=={224} (set/a prefixlen=%prefixlen%+3)
     If {%1}=={240} (set/a prefixlen=%prefixlen%+4)
     If {%1}=={248} (set/a prefixlen=%prefixlen%+5)
     If {%1}=={252} (set/a prefixlen=%prefixlen%+6)
     If {%1}=={254} (set/a prefixlen=%prefixlen%+7)
     If {%1}=={255} (set/a prefixlen=%prefixlen%+8)
     Set "digit=%1"
     If defined digit (goto :repeat )
    Goto:eof --------------------
    rem TEST
    echo.%strIP%/%prefixlen% [%SubnetMask%]

    Last edited by Rems; 13th October 2008, 23:30.

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts