Hi all,
Still trying to get my feet wet with PS. Have a need to search AD for any groups with a specific term in the group name then return the users of those groups as well as if the accounts are enabled. I know there are always multiple ways of solving a problem with scripting so I'm looking for guidance on how make my script more efficient as well as acheive what I'm trying to do. So my initial script is below:
######## Function to find groups with the word Admin in it #####
Function GRPWITHADMIN
{
$ADMGROUPS = get-adgroup -filter {Name -like "*Admin*"} | sort | Select Name
}
############## Function to Expand Group Membership #############
Function GMEMExpanded
{
$colMembers=GRPWITHADMIN
Foreach ($colMember in $colMembers)
{
$objUName = get-aduser $colMember | select Name
$objEnabledStatus = get-aduser $colMember | select Enabled
}
$colMembersExpanded
}
############################### MAIN ###########################
$SelGroups = GRPWITHADMIN
Foreach ($SelGroup in $SelGroups)
{
Write-Host ""
Write-Host "Enumerating $SelGroup.." -foregroundColor yellow
$colMembersExpanded = @()
$members = GMEMExpanded $SelGroup
ForEach ($user in $members)
{
$user.Name
"Status: "$user.Enabled
}
}
$colMembersExpanded | Export-CSV ".\AllAdminUsers.csv"
Obviously the script is not complete. I stopped at the end when cycling through the members where I realized that I don't have an easy way to pull the user attribute without some how loading it/tying it to the user. The ForEach $user array, i'm trying to use $user.Name when, looking at it, there is now way the "Name" AD attribute is defined anywhere so that call is useless as far as the script goes. I guess this is where my lack of knowledge comes in.
So my ultimate outcome would be to query AD with any group with the string "Admin" in it. Take those groups, enumerate the users and then output the results to something like:
Group Name: WebAdmins
User: jsmith_admin; Enabled: True
User: psmith_admin1; Enabled: False
Group Name: DBAdmins
User: mchen.admin; Enabled: True
etc....
Thanks in advance for the guidance.