简体   繁体   中英

How to dynamically move computer accounts from Computers OU

Its pretty "simple" what i want to achieve. I have people creating Computer Objects on my AD and leaving there without moving them to the appropiate OU.

I would like a powershell script to read the list of computers from the Computers OU, and depending the first 5 or 6 letters from the Computer name, move it to the appropiate OU, reading the list of destination OUs from a CSV or txt or whatever file type.

I need to move more than 100 computers and I would like to scan them, and move them to their corresponding OU.

I've thought to use a variable for the computer accounts, then a foreach and a switch or something similar, and 1-by-1 start moving the accounts. But I'm stuck.

Thank you!!!!

This should be dynamic enough. You can replace the Map object with a CSV.

$Map = [PSCustomObject]@{
        AABBCC = "OU=ABC,DC=Contoso,DC=com";
        CCBBAA = "OU=CBA,DC=Contoso,DC=com"
    }

    $Prefixlist = ($Map.PSObject.Members | Where-Object { $_.MemberType -eq "NoteProperty" }).Name
    $Report = @()
    $MissingPrefix = @()

    Get-ADComputer -filter * -searchbase "CN=Computers,DC=Contoso,DC=com" -Properties Name | ForEach-Object {
        $obj = $_
        $Prefix = ($obj.Name).Substring(0, 6)
        if ($Prefixlist -contains $Prefixlist) {
            try {
                $obj | Move-AdObject -Targetpath $Map.$Prefix -erroraction stop
                $Report += [PSCustomObject]@{
                    Name = $Obj.Name
                    Move = $true
                }
            }
            catch {
                $_.Exception.ErrorRecord
                $Report += [PSCustomObject]@{
                    Name = $Obj.Name
                    Move = $false
                }
            }

        }
        else {
            $MissingPrefix += $Prefixlist
            $Report += [PSCustomObject]@{
                Name = $Obj.Name
                Move = $false
            }
        }
    }

    "Result"
    $Report | Format-Table -AutoSize

    "Not found prefix list"
    $MissingPrefix

Option 2 to make the path based on the prefix

$Report = @()

Get-ADComputer -filter * -searchbase "CN=Computers,DC=Contoso,DC=com" -Properties Name | ForEach-Object {
    $obj = $_
    $Prefix = ($obj.Name).Substring(0, 6)

    try {
        $obj | Move-AdObject -Targetpath "OU=Computers,OU=$Prefix,DC=Contoso,DC=com" -erroraction stop
        $Report += [PSCustomObject]@{
            Name = $Obj.Name
            Move = $true
        }
    }
    catch {
        $_.Exception.ErrorRecord
        $Report += [PSCustomObject]@{
            Name = $Obj.Name
            Move = $false
        }
    }

}


"Result"
$Report | Format-Table -AutoSize

Turning my comment into an answer. You could create a lookup Hashtable for this:

# create a lookup Hashtable for all OU's in your organisation
# You can limit this using parameters like '-SearchScope' and '-SearchBase' depending on the structure in your AD environment
$allOUs = @{}
Get-ADOrganizationalUnit -Filter 'Name -like "*"' | ForEach-Object {
    $allOUs[$_.Name] = $_.DistinguishedName 
}

# next, get all computers in the default Computers OU
$result = Get-ADComputer -Filter * -SearchBase "CN=Computers,DC=Contoso,DC=com" | ForEach-Object {
    $computerName = $_.Name
    $found = $false
    if ($computerName.Length -ge 6) {
        $targetOU = $computerName.Substring(0,6)
        $found    = $allOUs.ContainsKey($targetOU)
    }
    if (!$found -and $computerName.Length -ge 5) {
        $targetOU = $computerName.Substring(0,5)
        $found    = $allOUs.ContainsKey($targetOU)
    }
    if ($found) {
        try {
            $_ | Move-ADObject -TargetPath $allOUs[$targetOU] -ErrorAction Stop -WhatIf
            # add success to the $result
            [PsCustomObject]@{
                'Computer' = $computerName
                'TargetOU' = $targetOU
                'Result'   = 'Moved'
            }
        }
        catch {
            # add exception to the $result
            [PsCustomObject]@{
                'Computer' = $computerName
                'TargetOU' = $targetOU
                'Result'   = 'Not moved. {0}' -f $_.Exception.Message
            }
        }
    }
    else {
        # add failure to the $result
        [PsCustomObject]@{
            'Computer' = $computerName
            'TargetOU' = ''
            'Result'   = 'Not moved. Computername does not begin with a valid OU name'
        }
    }
}

# output on screen
$result

# output to file
$result | Export-Csv -Path 'ComputersMoved.CSV' -NoTypeInformation

Remove the -WhatIf switch if you are satisfied with the results shown in the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM