【问题标题】:Loop issue, script not adding the correct members to groups循环问题,脚本未将正确的成员添加到组
【发布时间】:2021-02-13 09:50:18
【问题描述】:

好的,先写代码:

 import-module activedirectory

. "C:\Users\ben\Documents\Get-Directreport.ps1"

$ou = "ou=test, ou=Litmos, ou=Resources, ou=Groups, ou=company, dc=domain, dc=net"

$creds = "domain.net\ben"
$server = "dc01.domain.net"

$managers = get-adGroupMember -identity "CN=All Managers,OU=Organizational,OU=Groups,OU=company,DC=domain,DC=net" | select name, samaccountname
$name = $managers.name

$ReportsTo = Get-adgroup -searchbase $ou -filter "Name -like 'Report to *'" | where {$_.name -replace 'Report to ' -in $name} | select name, samaccountname
$Reports = $reportsto.name

$underlings = Get-Directreport $name | select samaccountname

Foreach ($manager in $name) { 
    if ($manager -notin ($reports -replace 'Report to ')) 
    { new-adgroup -name "Report to $manager" -groupscope global -path $ou }
 }   
 

ForEach ($report in $reports) {
    #Get-Directreport $name | select samaccountname
    {add-adgroupmember -identity $report -members $underlings.samaccountname}
} 

因此,该脚本正确地创建了“向 $name 报告”组,并且它正在添加成员,但是它创建的所有 300 个组都具有相同的成员集。我尝试将 $underlings 行放在最后一个循环中,但这导致根本没有成员。我很确定那条线是罪魁祸首,但我想不通。

谢谢!

编辑

首先为我最初遗漏的内容道歉。我想这就是我在一天结束时匆匆忙忙的结果。无论如何,所以这段代码试图创建一系列新的 AD 安全组,即“Report to x”提到的。它通过遍历“All Managers”组的成员来创建这些文件夹,并且 X 变量将是每个 ALL Manager 成员的名称值。 然后我试图用相应的用户填充这些组。这是失败的地方。 Get-Directreport $name | select samaccountname 的行实际上返回了它应该返回的内容。我可以在终端中运行包括 $underlings 在内的整行,它会正确返回。 我同意 Colyn1337 的观点,就是那条线。我不确定如何让它迭代。

【问题讨论】:

  • 欢迎来到 StackOverflow。此评论包含重要反馈和您的答案。为了可读性,在下一行继续管道操作。 Long one liner 可能会让你在 syseng 中获得荣誉,但不会在 dev 中获得荣誉。检查飞溅。这两件事都使您的代码更易于阅读。下一件大事是,您的帖子没有明确定义该代码试图实现的目标,从而迫使读者推断它。因此,我无法修复您的代码。话虽如此,您的问题在于“$Underlings”。它没有被迭代,所以它有相同的对象数组。
  • Get-Directreport $name | select samaccountname 是否返回您所测试的任何 $name 值的预期值?
  • 我根据 Colyns 的评论进行了更改,对此感到抱歉:)。 AdminOfThings,是的!准确返回我正在寻找的内容。

标签: powershell scripting


【解决方案1】:

所以在完成脚本一段时间后,我提供了一些更改和内联 cmets。请看一下

import-module activedirectory

. "C:\Users\ben\Documents\Get-Directreport.ps1"

$ou = "ou=test, ou=Litmos, ou=Resources, ou=Groups, ou=company, dc=domain, dc=net"

$creds = "domain.net\ben"
$server = "dc01.domain.net"

$managers = get-adGroupMember -identity "CN=All Managers,OU=Organizational,OU=Groups,OU=company,DC=domain,DC=net" | 
    Select-Object name, samaccountname

# remove this and just use $managers.name.  No need to create this.  
## $name = $managers.name 

# Groups where name like "reports to $managersName"
$ReportsTo = Get-adgroup -searchbase $ou -filter "Name -like 'Report to *'" | 
    Where-Object { $_.name -replace 'Report to ' -in $name } | 
    Select-Object name, samaccountname

# Same as before.  No need for $Reports array.  Just use $ReportsTo.Name were needed
## $Reports = $reportsto.name  # AD Group names like "reports to <manager>'   


# list of  users for all managers?  Does Get-Directreport take an array of managers or does it expect only 1 manager?
# If sending multiple managers does work, which it sounds like it doesn't, but if it did you would have an array of all direct reports 
# for all managers in $name.  This should probably be moved into the foreach ($manager in $name) loop
## $underlings = Get-Directreport $name | Select-Object samaccountname  

Foreach ($manager in ($managers.Name))) { 
    # Creates  missing "Report to <manager>" groups
    if ($manager -notin (($ReportsTo.Name) -replace 'Report to ')) { 
        new-adgroup -name "Report to $manager" -groupscope global -path $ou 
    }

    # Get managers direct reports
    $underlings = Get-Directreports $manager | Select-Object samaccountname

    # Get manager's "report to <manager> group again to update memebers"
    $managerReportToGroup = Get-ADGroup -SearchBase $ou -Filter "Name -like 'Report to $manager'"
    if ($managerReportToGroup) {
        Add-ADGroupMember -identity $managerReportToGroup.Name -members $underlings.samaccountname
    } else {
        Write-Warning "Could not locate group for $manager"
    }

}   

# For each "reports to $managerName" group, adding direct reports found from Get-Directreports function/script
# $reports will not have any newly added groups from previous loop.  Would need to call Get-AdGroup again
# Recommend moving loop up into $manager loop
## ForEach ($report in $reports) {
##     #Get-Directreport $name | select samaccountname
##     { add-adgroupmember -identity $report -members $underlings.samaccountname }
## } 

【讨论】:

  • 非常非常接近。所以 Get-DirectReport "sameaccountname" | select Samaccountname 返回 AD 中直接下属属性的列表。我认为那将是一个数组,但说实话,我现在还不够聪明,无法知道一个列表或数组。上面的代码确实生成了一组错误,即它抛出了一个适合 Reprot To 组已经存在的错误。但不仅如此,调用 Get-Directreport 函数并不能正常工作。我将尝试将其整个代码粘贴到功能块中,看看是否有帮助。我在做点源的时候都错了。
  • 实际上在您stackoverflow.com/users/11954025/daniel 的大力帮助下让它工作了,我确实不得不做一些非常小的改变。即删除 $managers.Name))) 中的第 3 个 ) 并更改 $managers def 删除 | name 只留下 samaccountname。之后她跑了!!非常感谢。
猜你喜欢
  • 1970-01-01
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多