none
Destination OU based on Computer name prefix RRS feed

  • Question

  • Using MDT 2013. I would like to automatically move a computer object based on the computer name prefix. We currently have 20 country codes and 20 OU's that correspond to each country. 

    So if a computer name starts with US it will be placed in a predefined OU. If a computer name starts with GB it will be placed in a predefined OU, etc...

    What is the best way to achieve this? This should happen all automatically and no user intervention.

    Friday, November 28, 2014 2:32 PM

Answers

  • I used this vbs script at the end of deployment. You will need to change the "runas" to a service account with rights to move objects in AD. You also need to adjust your control chars and case. It has work like a charm for me, good luck.

    On Error Resume Next
    'Get MachineObjectOU Value
    Set wshNetwork = CreateObject("WScript.Network")
    Set uFso = CreateObject("Scripting.FileSystemObject")
    Set objSysInfo = CreateObject( "ADSystemInfo" )
    Set ArgObj = WScript.Arguments
    strComputerDN = objSysInfo.ComputerName
    nComma = InStr(strComputerDN,",")
    strCurrentOU = Mid(strComputerDN,nComma+1)
    strComputerName = Left(strComputerDN,nComma - 1)
    'Lets obtain the 4 control chars.
    ctrlchars = ucase((mid(strComputerName,9,4)))
    
    
    'Check result, drop into Undefined if no match.
    Select Case ctrlchars
    'Let check the school for staff laptops we looks at the building name 
    Case "001"
      strMachineObjectOU="OU=Library Cart,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "002"
      strMachineObjectOU="OU=Library Cart,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "003"
      strMachineObjectOU="OU=Library Cart,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "004"
      strMachineObjectOU="OU=Library Cart,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A115"
      strMachineObjectOU="OU=A115,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A121"
      strMachineObjectOU="OU=A121,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A123"
      strMachineObjectOU="OU=A123,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A128"
      strMachineObjectOU="OU=A128,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A129"
      strMachineObjectOU="OU=A129,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A133"
      strMachineObjectOU="OU=A133,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A134"
      strMachineObjectOU="OU=A134,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A170"
      strMachineObjectOU="OU=A170,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A220"
      strMachineObjectOU="OU=A220,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A222"
      strMachineObjectOU="OU=A222,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A231"
      strMachineObjectOU="OU=A231,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A233"
      strMachineObjectOU="OU=A233,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A261"
      strMachineObjectOU="OU=A261,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "A315"
      strMachineObjectOU="OU=A315,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "C240"
      strMachineObjectOU="OU=C240,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    Case "SPED"
      strMachineObjectOU="OU=A315,OU=Computers,OU=school,OU=Students,DC=domain,DC=com"
    
    Case Else
      'The format does not match any category, so we place the computer in a Undefined-ou.
      strMachineObjectOU="OU=Computers,DC=domain,DC=com"
    End Select
    'If current ou is different than target OU. Move object
    If UCase(strCurrentOU) <> UCase(strMachineObjectOU) Then
    Set objNewOU = GetObject("LDAP://" & strMachineObjectOU)
    Set objMoveComputer = objNewOU.MoveHere("LDAP://" & strComputerDN, strComputerName)
    End If

    Monday, December 1, 2014 5:22 PM