locked
Moving computers into multiple OUs RRS feed

  • Question

  • Hi,

    I'm having issues with a script I'm trying to get a script to move computers into multiple OUs. I'm currently using a CSV file that is automatically exported from record keeping software, I'm currently using these three columns:

    STKEY,SCHOOL_YEAR,STATUS

    ZAM0001,11,LVNG
    ZAM0002,12,FUT
    ZAM0004,11,LEFT
    ZAM0005,4,LEFT
    ZAM0008,7,LEFT
    ZAM0010,10,ACTV
    ZAM0012,7,ACTV
    ZAM0015,0,ACTV
    ZAM0016,1,ACTV

    The script does work if all computers are unsorted from their OU but if I choose to run the script again, they get moved around to different OUs. ZAM0012 is the only user with a computer and should be put in the Year_07 ou but gets moved to Year_00 if I run the script again. I wanted to be able to run the script throughout the year to make sure that computer are put into the right OU.

    This is the some of the output I am getting.

    Computer account ZAM0012 has been moved to Year_07 successfully
    Get-ADComputer : Cannot find an object with identity: 'ZAM0015' under: 'DC=wan'.
    At C:\Users\ben\Desktop\laptops\One_file_script.ps1:23 char:17
    +     $computer = Get-ADComputer $item.STKEY
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (ZAM0015:ADComputer) [Get-ADComputer], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

    Computer account ZAM0012 has been moved to Year_00 successfully
    Get-ADComputer : Cannot find an object with identity: 'ZAM0016' under: 'DC=wan'.
    At C:\Users\***\Desktop\laptops\One_file_script.ps1:33 char:17
    +     $computer = Get-ADComputer $item.STKEY
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (ZAM0016:ADComputer) [Get-ADComputer], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

    Move-ADObject : Directory object not found
    At C:\Users\***\Desktop\laptops\One_file_script.ps1:35 char:5
    +     Move-ADObject -Identity $computer.DistinguishedName -TargetPath $ ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (CN=ZAM0012,OU=Y...e,DC=wan:ADObject) [Move-ADObject], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

    Computer account ZAM0012 has been moved to Year_01 successfully
    Get-ADComputer : Cannot find an object with identity: 'ZAW0006' under: ',DC=wan'.
    At C:\Users\***\Desktop\laptops\One_file_script.ps1:123 char:17
    +     $computer = Get-ADComputer $item.STKEY
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (ZAW0006:ADComputer) [Get-ADComputer], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

    Move-ADObject : Directory object not found
    At C:\Users\***\Desktop\laptops\One_file_script.ps1:125 char:5
    +     Move-ADObject -Identity $computer.DistinguishedName -TargetPath $ ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (CN=ZAM0012,OU=Y...e,DC=wan:ADObject) [Move-ADObject], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

    Computer account ZAM0012 has been moved to Year_10 successfully

    This is how the script I am currently using.

                 

    Import-Module ActiveDirectory
    $ComputersPath= Import-Csv -Path "C:\Users\***\Desktop\laptops\file.csv"
    $TargetOU0 = "OU=Year_00,OU=One to One,OU=Computers,dc=..."
    $TargetOU1 = "OU=Year_01,OU=One to One,OU=Computers,dc=..."
    $TargetOU2 = "OU=Year_02,OU=One to One,OU=Computers,dc=..."
    $TargetOU3 = "OU=Year_03,OU=One to One,OU=Computers,dc=..."
    $TargetOU4 = "OU=Year_04,OU=One to One,OU=Computers,dc=..."
    $TargetOU5 = "OU=Year_05,OU=One to One,OU=Computers,dc=..."
    $TargetOU6 = "OU=Year_06,OU=One to One,OU=Computers,dc=..."
    $TargetOU7 = "OU=Year_07,OU=One to One,OU=Computers,dc=..."
    $TargetOU8 = "OU=Year_08,OU=One to One,OU=Computers,dc=..."
    $TargetOU9 = "OU=Year_09,OU=One to One,OU=Computers,dc=..."
    $TargetOU10 = "OU=Year_10,OU=One to One,OU=Computers,dc=..."
    $TargetOU11 = "OU=Year_11,OU=One to One,OU=Computers,dc=..."
    $TargetOU12 = "OU=Year_12,OU=One to One,OU=Computers,dc=..."
    $TargetOUDisabled = "OU=Disabled,OU=Computers,OU=Students,dc=..."

    foreach ($item in $ComputersPath){

    IF ($item.SCHOOL_YEAR -ceq "0" -and ($item.STATUS -eq "ACTV" -or $item.STATUS -eq "FUT"))
        {

        $computer = Get-ADComputer $item.STKEY

        Move-ADObject -Identity $computer.DistinguishedName -TargetPath $TargetOU0 -Confirm:$false

        Write-Host Computer account $computer.Name has been moved to Year_00 successfully
        }

    ELSEIF ($item.SCHOOL_YEAR -ceq "1" -and ($item.STATUS -eq "ACTV" -or $item.STATUS -eq "FUT"))
        {

        $computer = Get-ADComputer $item.STKEY

        Move-ADObject -Identity $computer.DistinguishedName -TargetPath $TargetOU1 -Confirm:$false

        Write-Host Computer account $computer.Name has been moved to Year_01 successfully
        }

    ELSEIF ($item.SCHOOL_YEAR -ceq "2" -and ($item.STATUS -eq "ACTV" -or $item.STATUS -eq "FUT"))
        {

        $computer = Get-ADComputer $item.STKEY

        Move-ADObject -Identity $computer.DistinguishedName -TargetPath $TargetOU2 -Confirm:$false

        Write-Host Computer account $computer.Name has been moved to Year_02 successfully
        }

     ELSEIF ($item.SCHOOL_YEAR -ceq "3" -and ($item.STATUS -eq "ACTV" -or $item.STATUS -eq "FUT"))
        {

        $computer = Get-ADComputer $item.STKEY

        Move-ADObject -Identity $computer.DistinguishedName -TargetPath $TargetOU3 -Confirm:$false

        Write-Host Computer account $computer.Name has been moved to Year_03 successfully
        }

    ect......

    I have removed some sensitive information from the script.


    • Edited by Ben Morrish Friday, September 7, 2018 6:23 AM
    Friday, September 7, 2018 5:19 AM

Answers

  • Difficult to say what's wrong. But I can't find problem in script. All I can help you - a bit optimize script. You have some repeatable actions, so you firstly need minimize actions, and at start exclude not active PC from file (you don't need check in every cycle if-else same parameters - it really slow perfomance). Try with it, if will some troubles:

    Import-Csv -Path "C:\Comps.txt" | where {$_.Status -eq "ACTV" -or $_.Status -eq "FUT"} | Export-Csv -Path "C:\compsNew.csv" -NoTypeInformation
    $Computers = Import-Csv "C:\compsnew.csv"
    $TargetOU = $(0..12 | %{"OU=Year_$_,OU=Target,DC=test,DC=local"})

    Foreach ($c in $Computers){
        switch ($c.school_year){
            "0" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[0] -Confirm:$false}
            "1" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[1] -Confirm:$false}
            "2" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[2] -Confirm:$false}
            "3" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[3] -Confirm:$false}
            "4" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[4] -Confirm:$false}
            "5" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[5] -Confirm:$false}
            "6" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[6] -Confirm:$false}
            "7" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[7] -Confirm:$false}
            "8" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[8] -Confirm:$false}
            "9" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[9] -Confirm:$false}
            "10" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[10] -Confirm:$false}
            "11" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[11] -Confirm:$false}
            "12" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[12] -Confirm:$false}
        }
    }

    PS. For more convenient naming OU in variable, I exclude "0" from naming with single-valued.


    • Edited by tolstyiii Monday, September 10, 2018 6:56 AM
    • Marked as answer by Ben Morrish Wednesday, September 12, 2018 4:03 AM
    Monday, September 10, 2018 6:53 AM

All replies

  • Hey @bucketsponge

    You haven't specified any delimiters when importing the csv file. Maybe this causes the issue. Looking at your CSV file you must specify "," as the delimiter. The import cmdlet will look like this:

    $ComputersPath= Import-Csv -Path "C:\Users\***\Desktop\laptops\file.csv" -Delimiter ","

    Here more information about specifying the delimiter: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-6

    Please let me know the results :)

    Take a bit and learn that the delimiter is only required when the delimiter is other than a comma.

    You can find this out by reading the help for the CmdLet.   Each CmdLet in PowerShell has a help and each parameter will show its default value.


    \_(ツ)_/

    Friday, September 7, 2018 7:22 AM
  • Hello,

    From information is presented above, script work fine. If you have changes in the next run time - it not a script, it some changes elsewhere. For example, there are some changes in import file. Check other possible ways of changes, or, if it not full script - need to see, what else are doing in script.

    Friday, September 7, 2018 9:36 AM
  • No the script is repetitive after that. There are no changes to the import file as I am currently using a copy of the file to test and get it working correctly before testing on the actual file. My only theory is that I might have to get a list of the computers that are currently in AD and some how cross reference the two. 1. I'm not sure it will work 2. I'm still learning powershell so it'll take me a while to work it out.

    My thinking is that
    If computer exists in AD then find in csv move to ou x.

    But not sure if it will work.


    • Edited by Ben Morrish Monday, September 10, 2018 12:24 AM
    Sunday, September 9, 2018 11:52 PM
  • Difficult to say what's wrong. But I can't find problem in script. All I can help you - a bit optimize script. You have some repeatable actions, so you firstly need minimize actions, and at start exclude not active PC from file (you don't need check in every cycle if-else same parameters - it really slow perfomance). Try with it, if will some troubles:

    Import-Csv -Path "C:\Comps.txt" | where {$_.Status -eq "ACTV" -or $_.Status -eq "FUT"} | Export-Csv -Path "C:\compsNew.csv" -NoTypeInformation
    $Computers = Import-Csv "C:\compsnew.csv"
    $TargetOU = $(0..12 | %{"OU=Year_$_,OU=Target,DC=test,DC=local"})

    Foreach ($c in $Computers){
        switch ($c.school_year){
            "0" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[0] -Confirm:$false}
            "1" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[1] -Confirm:$false}
            "2" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[2] -Confirm:$false}
            "3" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[3] -Confirm:$false}
            "4" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[4] -Confirm:$false}
            "5" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[5] -Confirm:$false}
            "6" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[6] -Confirm:$false}
            "7" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[7] -Confirm:$false}
            "8" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[8] -Confirm:$false}
            "9" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[9] -Confirm:$false}
            "10" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[10] -Confirm:$false}
            "11" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[11] -Confirm:$false}
            "12" {Get-ADComputer $c.STKEY | Move-ADObject -TargetPath $TargetOU[12] -Confirm:$false}
        }
    }

    PS. For more convenient naming OU in variable, I exclude "0" from naming with single-valued.


    • Edited by tolstyiii Monday, September 10, 2018 6:56 AM
    • Marked as answer by Ben Morrish Wednesday, September 12, 2018 4:03 AM
    Monday, September 10, 2018 6:53 AM