We help IT Professionals succeed at work.

Help with Powershell script that gets AD group and members' computer name based on Computer description match

garryshape
garryshape asked
on
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 report
foreach ($Group in $CertainGroups) {

$Name = $Group.Name
$GroupMembers = get-adgroupmember $Group.SID | select -expand samaccountname
write-host $Name -foregroundcolor "Black" -backgroundcolor "White"

$MemberComputer = foreach ($member in $GroupMembers) { $ADComputers | ? {$_.Description -like "$member*"} }
$MemberComputer | select Name,Description
}    

Open in new window

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 )
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This should do it:
$groupFilter = 'CertainPrefix*'
Get-ADGroup -Filter "Name -like '$($groupFilter)'" -Properties Name, Description | ForEach-Object {
	$group = $_
	Write-Host "Group $($group.Name)" -ForegroundColor Black -BackgroundColor White
	$group | Get-ADGroupMember | Select-Object -ExpandProperty SamAccountName | ForEach-Object {
		Get-ADComputer -Filter "Description -like '$($_)*'" -Properties Name, Description | ForEach-Object {
			Write-Host "    $($_.Name): $($_.Description)" -ForegroundColor Black -BackgroundColor White
			$_ | Select-Object -Property @{n='Group'; e={$group.Name}}, Name, Description
		}
	}
} | Export-Csv -NoTypeInformation -Path "C:\Temp\GroupReport.csv"

Open in new window

Author

Commented:
Bro brilliant! Thanks so much. Do you do paid services here? Love to work with you to help me more.