Moving Computer Account using a script

Home Forums Scripting General Scripting Moving Computer Account using a script

This topic contains 1 reply, has 2 voices, and was last updated by  Rems 10 years, 1 month ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts

  • totoy bato
    Member
    #135849

    Is it possible to move a computers using vbscript /batch by tracing their IP.
    In my company we have a subnet per area.
    eg.
    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 !!!) :bowdown::bowdown::bowdown:.

    We need the following:
    1: Relocating Computer Accounts using vbscript/bat to their respective OU’s by
    tracing their IP’s
    Ex.
    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.

    Thanks

    TOTOY BATO
    =========
    E-HEADS For EVER!


    Rems
    Moderator
    #227116

    Re: Moving Computer Account using a script

    totoy bato;125976 wrote:
    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.
    Or
    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.

    Code:
    :: 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” )
    )
    :prefixlen
    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”
    :repeat
    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)
    Shift
    Set “digit=%1”
    If defined digit (goto :repeat )
    Goto:eof ——————–

    :done2
    rem TEST
    echo.%strIP%/%prefixlen% [%SubnetMask%]
    pause>nul

    Rems

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.