如何为ADD-Adgroupmember结果创建日志

时间:2015-08-10 22:02:26

标签: powershell-v4.0

简单来说,我已经准备好了PS脚本,但我想通过Add-ADGroupMember管理向AD安全组添加1000个名称的结果。问题是ADD-Adgroupmember不返回任何结果。最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以将来电包裹在try / catch区块中:

$Group = Get-ADGroup "myGroup"
$Users = "habanagold","habanasilver","mrceo"
foreach($User in $Users){
    try {
        Add-ADGroupMember -Identity $Group -Members $User -ErrorAction Stop
        "$User added to $Group" | Out-File C:\path\to\output.log -Append
    } catch {
        "Failed to add $User to $Group" | Out-File C:\path\to\output.log -Append
    }
}