Powershell移动AD对象

时间:2016-05-20 08:33:55

标签: powershell object active-directory move

我制作了这个脚本,找到所有未放置在正确OU中的Windows 10机器,此时没有采取任何措施 - 但是我想移动它们一旦找到它们,我们有超过30个国家和因此我希望将OU字符串保留在数组中,以使代码保持最小 - 如何在此脚本中执行移动?我可以使用一些指针。

$Script:OUBase = "OU=Countries,OU=Global,DC=internal"
    Import-Module ActiveDirectory



    $CountryDataCenter = 
    @(
    [pscustomobject]@{Country="UK";DataCenter="CEN1"},
    [pscustomobject]@{Country="UK";DataCenter="CEN2"} 
    )


    Function GetWin10MachineAccounts($Country, $DataCenter){


    #Build OUstring
    $OUStringTarget = "*OU=Windows 10,OU=Computers,OU=" + $DataCenter + ",OU=" + $Country + "," + $Script:OUBase
    $OUStringSource = "OU=Computers,OU=" + $DataCenter + ",OU=" + $Country + "," + $Script:OUBase
    $countPC    = ($Win10Computeraccounts).count


    Write-Host "OU to search - " $OUStringSource -ForegroundColor Yellow


    $Win10ComputerAccounts = Get-ADComputer -SearchBase $OUStringSource -Filter {(enabled -eq "true") -and (OperatingSystem -like "*Windows 10*")} -properties * | where {$_.DistinguishedName -notlike "$OUStringTarget"} | select CN -expandproperty Name 


    Return $Win10Computeraccounts




    }



    ############### Main Script ##########################

    ##create empty array for use later
    $DataArray = @()

    ForEach ($Country in $CountryDataCenter)
    {
        $Win10Computeraccounts = GetWin10MachineAccounts $Country.Country $Country.DataCenter 
        $countPC    = $Win10Computeraccounts.count




        if(!$Win10Computeraccounts) {
         write-host "No Windows 10 Computers are found in the container" $Country.Country $Country.DataCenter
        }

        foreach ($Computer in $Win10Computeraccounts){     
            Write-Host $Computer -ForegroundColor Red
            #Store Data in foreach array
            $DataArray += (Get-ADComputer $Computer )
            Write-Host "$countPC" "Computers found in" $Country.Country $Country.DataCenter -ForegroundColor Green


        } 



    }



    $DataArray | Export-Csv "C:\log.csv"  -Force

1 个答案:

答案 0 :(得分:0)

使用Move-ADObject cmdlet

foreach($Country in $CountryDataCenter)
{
    $OUStringTarget = "OU=Windows 10,OU=Computers,OU={0},OU={1},{2}" -f $Country.DataCenter,$Country.Country,$Script:OUBase
    $Win10Computeraccounts = GetWin10MachineAccounts $Country.Country $Country.DataCenter 

    foreach ($Computer in $Win10Computeraccounts){     
        Move-ADObject -Identity $Computer -TargetPath $OUStringTarget
    } 
}