【发布时间】:2019-10-14 15:00:21
【问题描述】:
我正在将用户的 OU 添加到组中,并且想要捕获:
- 已添加用户
- 已在列表中的用户
- 将以上内容输出到文件中
下面的当前代码工作和输出:
用户名组名时间戳 -------- --------- --------- %用户名%%组名% 14/10/2019 15:50:49但是,catch 不会捕获组中已经存在的用户并报告控制台上的输出。导出到 CSV 仅包含 OU 中的所有用户并显示它添加的时间。
我希望catch 输出在屏幕上触发,并在导出的 CSV 文件中显示用户是否已添加或在此运行中添加。
我正在使用的代码:
$groupName = 'SOMEGROUP'
$ou = 'OU=Users,DC=DC,DC=LOCAL'
$cred = Get-Credential -Credential bsg\myusername$
$results = Get-ADUser -Filter * -SearchBase $ou -Credential $cred | ForEach-Object {
#Add the user to the group here
$userName = $_.Name
try {
Add-ADGroupMember -Identity $groupName -Members $_.DistinguishedName -Credential $cred -ErrorAction Stop
} catch {
Write-Warning "User $userName is already a member of group $groupName"
}
# output a PsCustomObject that gets collected in the $results variable
[PsCustomObject]@{
'UserName' = $userName
'GroupName' = $groupName
'TimeStamp' = Get-Date
}
}
# output on console
$results | Format-Table -AutoSize
# Export to CSV file
$results | Export-Csv C:\PS\AddADGroupToUsers.csv -NoTypeInformation
Read-Host -Prompt "Press Enter to exit"
我不确定Add-ADGroupMember。重要的是那里的 DistingushedName 而不是 $userName?
我希望输出显示无效的catch。
我在两个语句中都使用了-Credential $cred,有没有更简单的方法可以让所有内容都以-Credential 作为会话而不是命令来运行?
回复已阅读并理解。 我运行了脚本。用户已经在组中,所以我希望他们都回来,因为“用户已经是组的成员......”。但它似乎出错了。
最新更新后的错误消息(该组已经有很多成员):
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null, empty, or an element of the
argument collection contains a null value. Supply a collection that does not contain any null values and then try the
command again.
At C:\PS\add to usersNEW2.ps1:28 char:57
+ ... oupMember -Identity $groupName -Members $_.DistinghuishedName -ErrorA ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
pMember
输出 CSV 不正确,因为没有添加用户,他们已经在其中。所以结果是错误的:
UserName GroupName TimeStamp Status
TheUser GROUPSActual 16/10/2019 15:23 User added successfully
所以远离 Theo 的代码,我尝试了 Ivan 的建议,但这需要很长时间:
# I'll use the DistinghuishedName because this is always unique in the forest
$currentMembers = Get-ADGroupMember -Identity $groupName | Select-Object -ExpandProperty DistinghuishedName
$results = Get-ADUser -Filter * -SearchBase $ou -credential $cred | ForEach-Object {
# test if the user is already a member by checking the array
If ((Get-ADGroupMember -Identity $groupname).distinguishedName -contains $_.distinguishedName) {
Write-Warning "User $userName is already a member of group $groupName"
}
else {
Add-ADGroupMember -Identity $groupName -Members $_.DistinguishedName -Credential $cred -ErrorAction Stop
}
# output a PsCustomObject that gets collected in the $results variable
[PsCustomObject]@{
'UserName' = $_.Name
'GroupName' = $groupName
'TimeStamp' = Get-Date
'Status' = $status
}
}
我认为它正在工作,但需要几分钟才能从 7 个结果中获得 3 个结果,而原始代码在几秒钟内就可以工作。 编辑:代码已完成,并已写入托管警告。我知道我可以调整状态部分以将错误添加到状态中。只是不应该花这么长时间!
时间戳相隔几秒钟,但现在:
TimeStamp
16/10/2019 16:12
16/10/2019 16:14
16/10/2019 16:15
16/10/2019 16:16
16/10/2019 16:17
16/10/2019 16:18
16/10/2019 16:19
【问题讨论】:
标签: powershell active-directory