按文件移动广告组

时间:2018-12-16 10:43:06

标签: powershell active-directory

我是PS的新手,正在开始我的第一步。
我有一个名为"C:\temp\used_groups.csv"的文件。
该文件具有由Powershell脚本填充的AD组的电子邮件地址,以检查365中正在使用哪个通讯组。
现在,我希望能够将它们移动到其他OU。

该文件具有一些广告组的电子邮件地址,如下所示:

"RecipientAddress"
"test@test.com"
"test1@test.com"
"test2@test.com"
  1. 是否可以仅通过广告组的电子邮件地址属性来移动广告组?
  2. 如何通过他们的电子邮件地址属性来解析论坛的sAMAccountName属性?

这是我尝试失败的尝试:

 $Groups=import-csv "C:\temp\used_groups.csv"
 ForEach ($Group in $Groups){ 
    Get-ADGroup -Filter "mail -like $Group "
    # rest of script.. not done yet. 
 }

2 个答案:

答案 0 :(得分:2)

使用CSV时,必须指定字段名称。脚本中的另一个问题是AD过滤器中没有引号。

尝试一下:

$Groups=import-csv "C:\temp\used_groups.csv"
ForEach ($Group in $Groups){ 
    (Get-ADGroup -Filter "mail -like '$($Group.RecipientAddress)'").samaccountname
    # rest of script.. not done yet. 
}

干杯

杰特·扬

答案 1 :(得分:2)

我会做这样的事情:

# put the DistinghuishedName of the destination OU here
$destinationOU = "OU=Test,DC=Fabrikam,DC=COM"

# read the CSV and grab the 'RecipientAddress' fields in an array
$emailAddresses = (Import-Csv "C:\temp\used_groups.csv").RecipientAddress
foreach ($email in $emailAddresses){ 
    $GroupToMove = Get-ADGroup -Filter "mail -like '$email'"
    if ($GroupToMove) {
        # Move-ADObject takes the 'DistinghuishedName' or the 'objectGUID' as Identity parameter
        # but it also works when piping the group object itself to it.
        $GroupToMove | Move-ADObject -TargetPath $destinationOU
        Write-Host "Moved group '$($GroupToMove.Name)'."
    } 
    else {
        Write-Warning "Could not find group with email address '$email'"
    }
}