使用 foreach 将 AD 计算机添加到组
Using foreach to add AD computers to groups
这可能是错误的方法,但我最近几天在 PowerShell 中试验 foreach
(我使用 PowerShell 的第 5 版)。
我正在寻找的是一种将我已有的计算机列表添加到我已有的 AD 组列表中的方法。所以我使用 Get-Content
导入 2 个 .txt 文件,我还了解到 PowerShell 中的 AD 组使用 -Identity
而不是名称 我不知道该决定的原因。但是我还是想出了这个:
$Apps = Get-Content C:\Scripts\Apps.txt
$Computers = Get-Content C:\Scripts\Computers.txt
foreach ($App in $Apps) {
Add-ADGroupMember $Apps -Identity $Computers
}
我的问题是 Apps.txt
文件中只有 1 个 AD 组。如果我添加更多的组,PowerShell 会在我身上变红,然后我的电脑就会开始崩溃。
在 Computers.txt
中,我在末尾列出了带有 $
的计算机帐户,它们在不同的行中,如下所示:
PC1$
PC2$
在 Apps.txt
中,AD 组位于单独的行中,没有任何逗号或分号或任何内容。
将Add-ADGroupMember $Apps -Identity $Computers
行中的$Apps
改为$App
,同时-Identity
参数为AD组名。您还需要为用户使用 -Members
参数。例如。
Add-ADGroupMember -Identity $App -Members $Computers
这可能是错误的方法,但我最近几天在 PowerShell 中试验 foreach
(我使用 PowerShell 的第 5 版)。
我正在寻找的是一种将我已有的计算机列表添加到我已有的 AD 组列表中的方法。所以我使用 Get-Content
导入 2 个 .txt 文件,我还了解到 PowerShell 中的 AD 组使用 -Identity
而不是名称 我不知道该决定的原因。但是我还是想出了这个:
$Apps = Get-Content C:\Scripts\Apps.txt
$Computers = Get-Content C:\Scripts\Computers.txt
foreach ($App in $Apps) {
Add-ADGroupMember $Apps -Identity $Computers
}
我的问题是 Apps.txt
文件中只有 1 个 AD 组。如果我添加更多的组,PowerShell 会在我身上变红,然后我的电脑就会开始崩溃。
在 Computers.txt
中,我在末尾列出了带有 $
的计算机帐户,它们在不同的行中,如下所示:
PC1$
PC2$
在 Apps.txt
中,AD 组位于单独的行中,没有任何逗号或分号或任何内容。
将Add-ADGroupMember $Apps -Identity $Computers
行中的$Apps
改为$App
,同时-Identity
参数为AD组名。您还需要为用户使用 -Members
参数。例如。
Add-ADGroupMember -Identity $App -Members $Computers