2016-05-20 56 views
-1

我讓這個腳本找到所有未放置在正確的OU中的窗口10機器,此時沒有采取任何行動 - 但是我想一旦找到它們就移動它們,我們有超過30個國家和數據中心,所以我想將OU字符串保留在數組中,以使代碼保持最小 - 如何在此腳本中執行移動操作?我可以使用一些指針。Powershell移動AD對象

$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 

回答

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 
    } 
} 
+0

謝謝,但我需要字符串目標爲每個位置是動態的 – Bendo1984

+0

它*爲*動態,注意'$ OUStringTarget'字符串是如何構建的 - 使用當前的「國家」和「數據中心」值。基本上和你的'GetWin10MachineAccounts'函數一樣,只是使用'-f'運算符而不是連接字符串 –

+0

Move-ADObject:無法在'OU = Countries,OU = Global,'下找到標識爲'VPC02'的對象。 DC =內部」。 在線:75 char:9 + Move-ADObject -Identity $計算機-TargetPath $ OUStringTarget + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:ObjectNotFound:(VPC02:ADObject)[Move-ADObject] ,ADIdentityNotFoundException + FullyQualifiedErrorId:ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.MoveADObject – Bendo1984