Help with Powershell script that gets AD group and members' computer name based on Computer description match
Summary:
This is an Active Directory environment with users, groups and computers. There is a logon script in the environment that updates computers with the username of the user logged into the computer.
Using that information, I am trying to piece together a report of certain AD Groups, and their members, along with computers that have a match of their username in their description.
So the output would show the Group name, and then the found computer object with the computer name and description. I don't need to output a username separately because that's already shown in the computer object description.
What I need help with:
I'd like to get it exported in a way that is a CSV. Right now it's just in the host, which is ok, but not easily shareable or to search like in Excel.
Just a general analysis of the script. It seems slow, maybe it's not right.
The script:
#Get all existing AD Groups$AllADGroups = Get-ADGroup -filter * -properties Name,Description #Gather groups I'm targeting that share a known common prefix. $CertainGroups= $AllADGroups | ? {$_.Name -like "CertainPrefix*"} | Select Name,SID #Get all AD Computers that we will check against once we have usernames. $ADComputers = get-adcomputer -filter * -properties Name,Description#iterate through the groups and come up with the reportforeach ($Group in $CertainGroups) {$Name = $Group.Name$GroupMembers = get-adgroupmember $Group.SID | select -expand samaccountnamewrite-host $Name -foregroundcolor "Black" -backgroundcolor "White"$MemberComputer = foreach ($member in $GroupMembers) { $ADComputers | ? {$_.Description -like "$member*"} }$MemberComputer | select Name,Description}
This then outputs something like in the host, but ideally put to Excel/CSV if there's a way: Group Name Here laptop01 john.doe (other info in computer description )
laptop02 pat.doe (other info in computer description )
desktop01 jane.doe (other info in computer description )