检查用户是否是广告组的成员-PowerShell

发布于 2025-01-24 03:02:52 字数 666 浏览 0 评论 0原文

我有一个带有用户名列表的TXT文件,我想检查这些用户是否是特定AD组的成员。如果他们不是该组的成员,则必须将其用户名编写在CSV文件中。因此,输出应该是一个CSV文件,其中没有指定的AD组中的所有用户名。

我总是会遇到“与要处理请求的目录连接的连接。这可能是瞬态条件。”目录模块与AD创建连接。

这是我的简短脚本:

 $userlist = get-content -Path "C:\Temp\users.txt"

 $group = "group_xy"


 $result = foreach ($user in $userlist)
 {
     $groupmembers = Get-ADgroup -Filter {Name -eq $group}|Get-ADGroupMember 

     if ($groupmembers.samaccountname -notmatch $users){
        [PSCustomObject]@{
        Name = $user 
        Group = $group 
        Member = 'False'
        }
    }
    
}

$result |Export-csv "C:\Temp\Result.csv" -NoTypeInformation

我该如何解决?

br

i have a txt file with a list of usernames and I want to check if these users are a member of a specific AD-group. If they aren't a member of this group, their username has to be written in a csv file. So the output should be a csv file with all usernames who are not in the specified AD-group.

I always get the error "A connection to the directory on which to process the request was unavailable. This is likely a transient condition".The error is reproduced when implementing a workflow that triggers up to 20 powershell sessions to run, each importing the Active Directory module to create a connection with AD.

This is my short script:

 $userlist = get-content -Path "C:\Temp\users.txt"

 $group = "group_xy"


 $result = foreach ($user in $userlist)
 {
     $groupmembers = Get-ADgroup -Filter {Name -eq $group}|Get-ADGroupMember 

     if ($groupmembers.samaccountname -notmatch $users){
        [PSCustomObject]@{
        Name = $user 
        Group = $group 
        Member = 'False'
        }
    }
    
}

$result |Export-csv "C:\Temp\Result.csv" -NoTypeInformation

How can I solve this?

BR

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2025-01-31 03:02:52
if ($groupmembers.samaccountname -notmatch $users) {

应该

if ($groupmembers.samaccountname -notmatch $user) {

这样

if ($groupmembers.samaccountname -notmatch $users) {

Should be

if ($groupmembers.samaccountname -notmatch $user) {

Since you never define $users I think this is the error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文