powershell ADD-ADGroupMember在计算机列表中的4个组中随机分配,4个组之间没有重复/重复

omqzjyyz  于 2023-06-06  发布在  Shell
关注(0)|答案(1)|浏览(375)

我下面的代码无法获取“$groupname“列表并在ADD-ADGroupmember步骤中运行它。我尝试了一种不同的方法,但我仍然以一台电脑显示在不同的组中结束。所有我想做的是随机添加计算机到一个选定数量的组,只允许每一个独特的计算机被分配到一个组?有什么想法吗

$logfile = "results-$((Get-Date).ToString('MM-dd-yyyy_hhmmtt')).log"

Get-Date | Out-File $logfile

$groupnameprefix = "Automated"

$organizationalunitpath="OU paths go here. There are multiple"

$grouporganizationalunitpath = "OU paths for AD Security Groups go here"
$NumberofComputersPerGroup = "300"
# This is a list of computer names that will be used to generate group names
$Patch_Groups = @(
"_Group_1",
"_Group_2",
"_Group_3",
"_Group_4"
)

# Get all computers from the specified organizational unit
[System.Collections.Generic.List[object]] $computers = $organizationalunitpath |
    ForEach-Object { Get-ADComputer -Filter * -SearchBase $_ }

# Create the new group
foreach ($Patch_Group in $Patch_Groups){
# Generate the group name by concatenating the prefix and the Patch Group name with spaces removed
$groupname = $groupnameprefix + $Patch_Group.Replace(" ","")
try {
# Create the group using the generated name and the specified organizational unit path
New-ADGroup -Name $groupName -path $grouporganizationalunitpath -GroupScope Global -verbose
} 

catch { 
         $message = "ADGroup $groupName already exists"
            $message | Out-File $logfile -Append
            Write-Warning $message
            Write-Warning $_.Exception.Message
            $_.Exception.Message | Out-File $logfile -Append
 }

    try {
        $AddComputerstoRandomADGroups = Get-Random -input $computers -count 300
        $computers = $computers | where {$_ -notin $AddComputerstoRandomADGroups}
        Add-ADGroupMember -Identity $groupname -Members $AddComputerstoRandomADGroups -verbose
    
    
    }
    catch {
        $message = ' A problem occurred trying to add Members'
        $message | Out-File $logfile -Append
        Write-Warning $message
        Write-Warning $_.Exception.Message
        $_.Exception.Message | Out-File $logfile -Append
    }
}
4c8rllxm

4c8rllxm1#

好吧,忽略我之前写的,我看到现在发生了什么。
你的代码中的Get-Random并不是选择一个随机的计算机,它只是选择一个随机数,然后将其传递到管道中。
假设$computers并不比等价的更复杂:

$computers=@()
$computers="computer1","computer2","computer3","computer4","computer5","computer6","computer7","computer8"

那么这段代码就可以实现这个目的:

$AddComputerstoRandomADGroups = Get-Random -input $computers -count 4
$computers = $computers | where {$_ -notin $AddComputerstoRandomADGroups}
Add-ADGroupMember -Identity $groupname -Members $AddComputerstoRandomADGroups -verbose

在本例中,它从$computers中当前的8台计算机中随机选择4台计算机,然后更新$computers变量,使其等于$computers中的所有计算机,除了分配给$AddComputerstoRandomADGroups的那些计算机,以便它们在未来的迭代中不会被选中。
编辑-如果你仍然没有发现脚本工作,我建议你需要输出实际发生的事情,以缩小你的问题所在。作为一个例子,我将您的代码简化为以下代码,该代码运行良好。

$organizationalunitpath="CN=Computers,DC=mydomain"
$Patch_Groups = @(
"_Group_1",
"_Group_2",
"_Group_3",
"_Group_4"
)
$groupnameprefix = "Automated"
[System.Collections.Generic.List[object]] $computers = $organizationalunitpath |
    ForEach-Object { Get-ADComputer -Filter * -SearchBase $_ }
foreach ($Patch_Group in $Patch_Groups){
    $groupname = $groupnameprefix + $Patch_Group.Replace(" ","")
   # write-host "New Group - Groupname = $groupname - path = $oupath"
    $AddComputerstoRandomADGroups = Get-Random -input $computers -count 20
    $computers = $computers | where {$_ -notin $AddComputerstoRandomADGroups}
   # write-host "add to $groupname computer $AddComputerstoRandomADGroups"   
   write-host "-- computers"
   $computers.name 
   write-host "-- added computers"
   $AddComputerstoRandomADGroups.name 
   write-host "---"
}

在四次迭代中的每一次迭代中,$computers变量中的条目数量都在减少,因此随机选择指定数量的计算机,可能会将其添加到组中,然后将这些计算机从列表中删除,因此它们不再可用于返回,您也可以从$AddComputerstoRandomADGroups的输出中看到,并且我在$AddComputerstoRandomADGroups的四个输出中没有看到任何机器多次出现。
因此,尝试运行它,看看您得到了什么输出,因为您可以尝试它,看看会发生什么,而无需实际创建组并向其中添加机器。

相关问题