Move computer object

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: HashMan (hashim.ismail_at_fac.com)
Date: 09/21/04


Date: Tue, 21 Sep 2004 08:21:06 -0700

Hi All;
I was wondering if you can point me to the right direction. I am trying to
write a script that will query the a list of computer and find who the users
are that are logged into them then move the computer object to user's OU. If
the user is logged into a computer resides in Accounting OU then I want the
computer object to move to that OU. Currently all the computer objects are
in Computer container. Thankx.

My Code thus far:
'\\\\Query Computer to list users that are logged on to them\\\\\
'\\\\Then query AD to locate user in the OU\\\\
'\\\\Move the computer object into user's OU\\\\

'\\\\Find the user logged on to the computer\\\\
dim strComputer, objConn, objWMIService, strName, objComputer, strUsername,
strQuery
dim strADspath, objUser
strComputer = inputbox("Enter the Path\Name of PC or Cancel to exit.", _
                    "PC Name")
Set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOOBJECT"
objConn.Open
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
strName=objComputer.UserName
Wscript.Echo strName

'\\\\Find the OU the user belongs to\\\\

'Set strUsername=strName
strUsername=inputbox("Enter Username.", strName)
strQuery= "SELECT ADsPath FROM 'LDAP://DC=domain,DC=com' WHERE
objectclass='User' and SAMAccountName = '" & strUserName & "'"
Set objRecordSet = objConn.Execute(strQuery)
strADspath=objRecordSet.Fields("ADsPath")
Set objUser = GetObject(strADspath)
objUser.GetInfo
wscript.echo "User Information for......." & objUser.Get("SAMAccountName")
wscript.echo "AD Path............." & strADspath

Set objNewOU = GetObject(strADspath2)
Set objMoveComputer = objNewOU.MoveHere _
    ("LDAP://CN=" & strComputer & ",CN=Computers,DC=fac,DC=com")
Next



Relevant Pages

  • Re: OU Help Please
    ... VBScript program that uses ADO to query AD: ... Dim adoCommand, adoConnection, strBase, strFilter, strAttributes ... Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName ... ' Search entire Active Directory domain. ...
    (microsoft.public.windows.server.active_directory)
  • Re: Possible Bug with TOP N clause in cursor definition. Re: Critical difference between behaviour
    ... Have you tried putting an ORDER BY clause in your query? ... select TOP 10 strName from myTable ... Then the second fetch would move to the second row before the first ...
    (comp.databases.sybase)
  • Re: Distinctrow
    ... SELECT strName, LAST, LAST ... just like a DISTINCT (for a SELECT query type). ... 5377 but I am still seeing information that is identical. ... Rick Brandt, Microsoft Access MVP ...
    (microsoft.public.access.queries)
  • RE: Printing query from form
    ... strName as String ... get to the database window to open and/or print a query or query results. ... parent form I've set up a button to save and open the results in an Excel ... The issue is that this opens the query in the background, ...
    (microsoft.public.access.modulesdaovba)
  • RE: Pruning Active Directory
    ... The computer pasword is reset ... computer object without an associated 'live' computer will have an old value ... For a simple query like this on a small domain, CSVDE or the new DS CLUs are ...
    (microsoft.public.windows.server.active_directory)