简体   繁体   English

PowerShell Active Directory 搜索

[英]PowerShell Active Directory search

I was tasked with developing a tool that Searches Active Directory to see all users within a group.我的任务是开发一个搜索 Active Directory 以查看组内所有用户的工具。 Now it is a bit more complex than that.现在它比这更复杂一些。 The tool needs prompt the user for input, based on that input it searches for a specific group matching the input.该工具需要提示用户输入,根据该输入搜索与输入匹配的特定组。 For example: I input T4 it pulls the T4Admin security group within the searched OU.例如:我输入 T4 它会在搜索到的 OU 中拉取 T4Admin 安全组。 This part I did without issue.这部分我没有问题。

Now, where I am stuck is I need to search for users containing a certain string "_OUAdmin" then pull those out into a variable.现在,我卡住的地方是我需要搜索包含某个字符串“_OUAdmin”的用户,然后将它们拉到一个变量中。

My code (excluding OU Path for security purposes):我的代码(出于安全目的不包括 OU Path):

#Prompts the user for OU Prefix and stores in the "ouPrefix" variable.
$searchPrefix = Read-Host -Prompt 'Please enter desired OU Prefix'

#Searches Active Directory for desired OU Prefix.
$selectedPrefix = Get-ADgroup -SearchBase "OU Path Cannot disclose" -Filter ('Name -like "*' + $searchPrefix + '*"')

#Pulls all group members from $selectedPrefix that contain "_OUAdmin". This is where I need help
#Help Needed Here!

#Removes "_OUAdmin" from all members.
#$modifiedAdmins =

#Appends "@test.com" to all members.
#$ouContacts =

#Displays Results of all OU Admin contacts in specified OU Prefix.
#Write-Host "OU Admins of '$searchPrefix' are $ouContacts"

I only need help in this one section called out "Help Needed Here.".我只需要这一部分的帮助,称为“此处需要帮助。”。

I am a beginner with PowerShell and therefore I am missing something probably simple, but any help or guidance is appreciated.我是 PowerShell 的初学者,因此我遗漏了一些可能很简单的东西,但感谢任何帮助或指导。

Here is a possible starting point for you:这是一个可能的起点:

$gm = @()

 ForEach ($member in $selectedPrefix) {
    $memberType = $member.objectClass
       If ($memberType -eq 'user' -and $member.SAMAccountName -like '*_OUAdmin*' ) {
            $gm += $member.name
            }
        }

The value of $gm should now equal each member of the groups that has OUAdmin their name in an array. $gm的值现在应该等于在数组中具有 OUAdmin 名称的组的每个成员。

Wouldn't it be:会不会是:

$selectedPrefix | Get-ADGroupMember | Where-Object {$_.name -like "*_OUAdmin*"}

Or am I missing something obvious还是我错过了一些明显的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM