none
Moving computer OU depending on OS RRS feed

  • Question

  • Hey Guys,

     I have been asked to write a script to pull a list of computer names from a CSV file and move the OU's depending on what version of Windows is on it. The script below runs with no errors but it does not move the computer OU's, I have tried is several times and so has my co-worker. Can anyone tell me where the error is?

    $Computers= Import-Csv "\\sof\temp\_Dustin\WIN 10 Rev.3.csv" | Where-Object { $_.Network -eq 'Net' } | Select Computername
    
    foreach ($C in $Computers) {
            try {
    
               Get-ADComputer $C.ComputerName -Properties Name,DistinguishedName | select Name,DistinguishedName | Export-Csv \\sof\temp\_Dustin\Original_OUs-Computers.csv -NoTypeInformation -Append 
              
               $OS = get-ADComputer $C.ComputerName -Properties OperatingSystem | Select -ExpandProperty OperatingSystem
    
                If($OS -eq "Windows 7 Enterprise"){
                            if ($C.ComputerName -Match "HQSW") {get-ADComputer $C.ComputerName | Move-ADObject -TargetPath "OU=Workstations"} 
                            if ($C.ComputerName -Match "HQSL") {get-ADComputer $C.ComputerName | Move-ADObject -TargetPath "OU=Laptops"} 
                            if ($C.ComputerName -Match "HQST") {get-ADComputer $C.ComputerName | Move-ADObject -TargetPath "OU=Tablets"} 
                            }
                If ($OS -eq "Windows 8.1 Enterprise"){
                            if ($C.ComputerName -Match "HQSW") {get-ADComputer $C.ComputerName | Move-ADObject -TargetPath "OU=Workstations"} 
                            if ($C.ComputerName -Match "HQSL") {get-ADComputer $C.ComputerName | Move-ADObject -TargetPath "OU=Laptops,"} 
                            if ($C.ComputerName -Match "HQST") {get-ADComputer $C.ComputerName | Move-ADObject -TargetPath "OU=Tablets,"} 
                            }
                If ($OS -eq "Windows 10 Enterprise"){
                            $C | Export-Csv \\sof\temp\_Dustin\Other_OS-Computers.csv -NoTypeInformation -Append}
    
                            } catch {$C | Export-Csv \\sof\temp\_Dustin\No_AD_Accounts-Computers.csv -NoTypeInformation -Append}
                 }
    

    Thanks Guys

    Monday, September 19, 2016 3:05 PM

Answers

  • I am not sure what happened but it worked when we ran it this time. I don't have an answer of why though, I didn't change the script at all.

    Thanks for your help though.

    • Marked as answer by Richardlaw Monday, September 19, 2016 5:38 PM
    Monday, September 19, 2016 5:38 PM

All replies

  • I suggest putting some trace statements into your if branches so you can see what's happening during execution.

    Are you sure the machines aren't moving and you're only seeing a replication delay?


    Monday, September 19, 2016 3:13 PM
  • I have waited for replication,  I made a small change and it ran but I got errors saying the objects parent was uninstantiated or deleted. I have checked the OU paths and they all seem correct.
    • Edited by Richardlaw Monday, September 19, 2016 3:20 PM
    Monday, September 19, 2016 3:18 PM
  • TargetPath must be the full DN.


    \_(ツ)_/

    Monday, September 19, 2016 3:56 PM
  • TargetPath must be the full DN.


    \_(ツ)_/

    I made the assumption that the OP removed most of the paths for brevity, but worth pointing out.


    Monday, September 19, 2016 4:06 PM
  • I have double-checked the DN,

    this is the target path with names changed for security purposes.

    {get-ADComputer $C.ComputerName | Move-ADObject -TargetPath "OU=Workstations,OU=N028,OU=OU1,OU=Windows 7,OU=Client Devices,OU=_SOF,DC=Domain3,DC=domain2,DC=domain1"} 

    I cant seem to figure this one out.

    Monday, September 19, 2016 4:23 PM
  • I am not sure what happened but it worked when we ran it this time. I don't have an answer of why though, I didn't change the script at all.

    Thanks for your help though.

    • Marked as answer by Richardlaw Monday, September 19, 2016 5:38 PM
    Monday, September 19, 2016 5:38 PM