簡體   English   中英

如何從計算機 OU 中動態移動計算機帳戶

[英]How to dynamically move computer accounts from Computers OU

我想要實現的非常“簡單”。 我有人在我的 AD 上創建計算機對象並離開那里而不將它們移動到適當的 OU。

我想要一個 powershell 腳本來讀取計算機 OU 中的計算機列表,並根據計算機名稱中的前 5 個或 6 個字母,將其移動到適當的 OU,從 CSV 或 txt 或其他任何內容中讀取目標 OU 列表文件類型。

我需要移動 100 多台計算機,我想掃描它們,並將它們移動到它們對應的 OU。

我曾想過為計算機帳戶使用一個變量,然后是一個foreach和一個switch或類似的東西,然后 1-by-1 開始移動帳戶。 但我被困住了。

謝謝!!!!

這應該足夠動態。 您可以用 CSV 替換 Map object。

$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

選項 2 根據前綴創建路徑

$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

將我的評論變成答案。 您可以為此創建一個查找哈希表:

# 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

如果您對控制台中顯示的結果感到滿意,請移除-WhatIf開關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM