Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何将各种组添加到OU中的计算机?_Powershell_Active Directory_Active Directory Group - Fatal编程技术网

Powershell 如何将各种组添加到OU中的计算机?

Powershell 如何将各种组添加到OU中的计算机?,powershell,active-directory,active-directory-group,Powershell,Active Directory,Active Directory Group,我需要将各种应用程序组添加到OU中的计算机,这些应用程序组将在稍后推出。在AD中,我转到OU,右键单击相应的计算机并单击属性,然后转到“成员”选项卡,然后添加各种组 如何使用PowerShell自动化这些步骤,以便将这些组应用于该OU中的所有计算机 import-module ActiveDirectory $allComputers = @() $ADgroup = "Computer Policy Application Group" $theOU = [ADSI]"LDAP://OU=

我需要将各种应用程序组添加到OU中的计算机,这些应用程序组将在稍后推出。在AD中,我转到OU,右键单击相应的计算机并单击属性,然后转到“成员”选项卡,然后添加各种组

如何使用PowerShell自动化这些步骤,以便将这些组应用于该OU中的所有计算机

import-module ActiveDirectory

$allComputers = @()
$ADgroup = "Computer Policy Application Group"

$theOU = [ADSI]"LDAP://OU=AnOU,DC=some,DC=test,DC=com"
foreach ($item in $theOU.psbase.Children) { 
    if ($item.ObjectCategory -like '*computer*') {
        $allComputers += $item.Name
    }
}

foreach ($pc in $allComputers) {
    Add-ADGroupMember $ADgroup $pc
}

当然,您可以添加更多的组,或者设置一个组数组,并在添加时对其进行迭代。。。顺便说一句,如果计算机已经是组的一部分,这将引发很多错误。

如果您使用的是server2008或更新版本(或安装了所需的组件),这是我找到的最简单的解决方案

$groupList=@("group1","group2","group3")
foreach ($Comp in (Get-AdComputer -server $ADServer -searchBase "OU=computers,DC=company,DC=com" -searchscope oneLevel")) {
    foreach ($Group in $groupList) { Add-ADGroupMember -Identity $Group -Members $Comp -Server $ADServer }
}

确保使用要添加的组的SAMAccountName数组填充$groupList变量,并用包含要添加权限的计算机的OU的LDAP路径替换“OU=computers,DC=company,DC=com”。

使用ActiveDirectory模块,您可以使用户添加ADPrincipalGroupMember或添加ADGroupMember

前者“将一个成员添加到一个或多个Active Directory组”,而后者“将一个或多个成员添加到Active Directory组”。

可能会为您提供一个起点。