Announcement

Collapse
No announcement yet.

Move Computer to new OU based on computer name

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

  • Move Computer to new OU based on computer name

    I currently have a script that will look at the machines OU and if it doesn't equal what I am looking for it will move the machine to that OU. I need to modify this script and I'm not sure how. I need the script to look at the machine name and if the machine name begins with "XD" move it to a designated OU. All other machines will go into a different OU. Here is what I am working with.
    Code:
    Set wshNetwork = CreateObject("WScript.Network")
    Set oFso = CreateObject("Scripting.FileSystemObject")
    Set objSysInfo = CreateObject( "ADSystemInfo" )
    Set ArgObj = WScript.Arguments
    'Use first argument as target OU
    strMachineObjectOU = "OU=Workstation TESTING,OU=Workstations,DC=NA,DC=ER,DC=COM"
    strComputerDN = objSysInfo.ComputerName
    nComma = InStr(strComputerDN,",")
    strCurrentOU = Mid(strComputerDN,nComma+1)
    wscript.echo strCurrentOU
    strComputerName = Left(strComputerDN,nComma - 1)
    '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
    Last edited by Rems; 9th May 2014, 19:48. Reason: added [code] tags round the code

  • #2
    Re: Move Computer to new OU based on computer name

    Originally posted by Poore View Post
    I currently have a script that will look at the machines OU and if it doesn't equal what I am looking for it will move the machine to that OU. I need to modify this script and I'm not sure how. I need the script to look at the machine name and if the machine name begins with "XD" move it to a designated OU. All other machines will go into a different OU. Here is what I am working with.
    Based on the existing code, it will be something like
    Code:
    Set objSysInfo = CreateObject("ADSystemInfo")
    
    strComputerDN = objSysInfo.ComputerName
    nComma = InStr(strComputerDN, ",")
    
    strCurrentOU = Mid(strComputerDN, nComma+1)
    strObjCN = Left(strComputerDN, nComma-1)
    
    If UCase(Mid(strObjCN,4,2)) = "XD" Then
       strMachineObjectOU = "OU=Workstation TESTING,OU=Workstations,DC=NA,DC=ER,DC=COM"
    Else
       strMachineObjectOU = "OU=Workstation OTHER,OU=Workstations,DC=NA,DC=ER,DC=COM"
    End If
    
    'If current ou is different than target OU. Move object
    If len(strMachineObjectOU) > 0 then
       If UCase(strCurrentOU) <> UCase(strMachineObjectOU) Then
         Set objNewOU = GetObject("LDAP://" & strMachineObjectOU)
         Set objMoveComputer = objNewOU.MoveHere("LDAP://" & strComputerDN, strComputerName)
       End If
    End If
    /Rems

    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