2016-05-20 23 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を使用してください: `$ OUStringTarget`文字列が構築されている様子がわかり、

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

のために動的にする文字列ターゲットを必要とする - 使用して現在の「国」と「データセンター」の値。あなたの 'GetWin10MachineAccounts'関数と基本的に同じです。文字列を連結する代わりに' -f'演算子を使用するだけです。 –

+0

Move-ADObject: 'OU = Countries、OU = Global'のID: 'VPC02' DC =内部 'である。ラインで :75文字:9 + -Identity $コンピュータ-targetpath $ OUStringTarget + ~~~~~~~~~~~~~~~~~~~~~~~~~~~-ADObjectを移動しますCategoryInfo:ObjectNotFound:(VPC02:ADObject)[Move-ADObject] 、ADIdentityNotFoundException + FullyQualifiedErrorId:ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException、Microsoft.ActiveDirectory.Management.Commands.MoveADObject – Bendo1984

関連する問題