Announcement

Collapse
No announcement yet.

Help to create VBScript to change OU

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help to create VBScript to change OU

    Trying to make a script to pull anything with the 'FC8' in the name in the Computers OU and put it in the UNMANAGED OU.

    Please help...this is what I have so far and it's not working...

    Code:
    ' MoveOU.vbs - Script = Not easy.
    ' VBScript to Move computers from the Computers to the unmanaged OU
    ' Author Trevor March
    ' -----------------------------------------------------------------'
    
    Option Explicit
    Dim objConnection, objCommand, objRecordSet, objNewOU, objMoveComputer
    Dim strComputer, strInitial, strSourceOU, strDestinationOU
    Dim intCounter
    
    ' Creates link to Active Directory
    Const ADS_SCOPE_SUBTREE = 2
    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand = CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    
    Set objCOmmand.ActiveConnection = objConnection
    
    ' Selects all the computer objects (not Users) in the Computers OU
    objCommand.CommandText = _
    "Select Name, Location from 'LDAP://OU=Computers,dc=gfoundries,dc=com'" _
    & "where objectClass='computer'"
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Timeout") = 30
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    objCommand.Properties("Cache Results") = False
    
    ' Treats the computer objects as a set of record cards
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    ' Get all the records
    Do Until objRecordSet.EOF
    strInitial = UCase(Left(objRecordSet.Fields("Name").Value,2))
    
    ' Filter Computers.
    If strInitial = "FC8" Then
    
    strComputer = objRecordSet.Fields("Name").Value
    strComputer = "CN="& strComputer
    
    intCounter = intCounter +1
    Wscript.Echo "Computer Name: " & strComputer
    
    strSourceOU = ",OU=Computers,DC=gfoundries,DC=com"
    strDestinationOU = "OU=UNMANAGED,DC=gfoundries,DC=com"
    
    Set objNewOU = GetObject("LDAP://" & strDestinationOU)
    Set objMoveComputer = objNewOU.MoveHere _
    ("LDAP://" & strComputer & strSourceOU, strComputer)
    
    ' Next line matches If strInitial
    End if
    
    objRecordSet.MoveNext
    
    Loop
    
    Wscript.Echo intCounter & " " & strInitial & " moved"
    
     
    ' End of VBScript
    Last edited by Rems; 24th February 2011, 19:57.

  • #2
    Re: Help to create VBScript to change OU

    What is the error you get?

    Double check the path to the computers CONTAINER (not actually an OU) -- I think it should be CN=COMPUTERS
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: Help to create VBScript to change OU

      Originally posted by Ossian View Post
      What is the error you get?

      Double check the path to the computers CONTAINER (not actually an OU) -- I think it should be CN=COMPUTERS
      Good catch Tom!


      Run the script below to test if computers are found. The script will not yet moving the computers.
      The next step,, remove the blue colored text and run the script. Verify the computers have moved.
      Code:
      SET oWshShell = createObject("WScript.shell")
      ' remove this and the above line after testing
      ' also - remove the checkpoint somewhere in the script
      '      - unremark the 4 rem'd out lines
      
      '----------------------------------------------------------------------'
      ' MoveOU.vbs - Script = Not easy.
      ' VBScript to Move computers from the Computers to the unmanaged OU
      ' Author Trevor March
      '----------------------------------------------------------------------'
      
      rem Option Explicit
      
      Dim objConnection, objCommand, objRecordSet, objNewOU, objMoveComputer
      Dim strComputer, strSourceOU, strDestinationOU
      Dim iCnt
      
      Dim oRE : Set oRE = New RegExp
      oRE.Global = True : oRE.IgnoreCase = True
      
      ' "^" means  "Only match the beginning of a string"
      oRE.Pattern = "^FC8"
      
      strSourceOU = "CN=Computers,DC=gfoundries,DC=com"
      strDestinationOU = "OU=UNMANAGED,DC=gfoundries,DC=com"
      
      rem Set objNewOU = GetObject("LDAP://" & strDestinationOU)
      
      ' Creates link to Active Directory
      Const ADS_SCOPE_SUBTREE = 2
      
      Set objConnection = CreateObject("ADODB.Connection")
      Set objCommand = CreateObject("ADODB.Command")
      objConnection.Provider = "ADsDSOObject"
      
      objConnection.Open "Active Directory Provider"
      Set objCOmmand.ActiveConnection = objConnection
      
      ' Selects all the computer objects in the Computers OU
      objCommand.CommandText = _
         "Select Name, Location from 'LDAP://" & strSourceOU & "'" _
         & "where objectClass='computer'"
      objCommand.Properties("Page Size") = 1000
      objCommand.Properties("Timeout") = 30
      objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
      objCommand.Properties("Cache Results") = False
      
      ' Treats the computer objects as a set of record cards
      Set objRecordSet = objCommand.Execute
      objRecordSet.MoveFirst
      
      ' Get all the records
      Do Until objRecordSet.EOF
      
         strComputer = objRecordSet.Fields("Name").Value
         If (oRE.test(strComputer) = True) Then
           iCnt = iCnt +1
           strComputer = "CN=" & strComputer
       
           'checkpoint
           oWshShell.popup ("Computer Name: " & strComputer),1 , iCnt
      
      rem     Set objMoveComputer = objNewOU.MoveHere _
      rem       ("LDAP://" & strComputer & "," & strSourceOU, strComputer)
      
         End if
      
         objRecordSet.MoveNext
      
      Loop
      objConnection.Close 
      Wscript.Echo iCnt, "computers moved"
      \Rems
      Last edited by Rems; 24th February 2011, 21:43.

      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

      Comment

      Working...
      X