GET-IT: TEAMS DAY | 1-Day Free Virtual Conference all about Teams. Here on Petri.com - 8/12/20 GET-IT: TEAMS DAY - 8/12/20

Help to create VBScript to change OU

Home Forums Scripting Windows Script Host Help to create VBScript to change OU

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    trevor.march
    Member
    #153552

    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…

    ‘ 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[/CODE][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[/CODE]

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.