简体   繁体   中英

PowerShell Active Directory search

I was tasked with developing a tool that Searches Active Directory to see all users within a group. 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. 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.

My code (excluding OU Path for security purposes):

#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.

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.

Wouldn't it be:

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

Or am I missing something obvious

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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