Ok, after working on this all morning I was able to get it working. Here is the script as it exists now. I also modified the CSV export so it would not include the header information.
$csvPath = "\\domain.local\dfs\STS\Scripts\Datafiles\TreeSize-Report.csv"
# Read the CSV file
$csvData = Import-Csv -Path $csvPath -Header "Username", "Allocated Size", "Number of Files", "Number of Folders", "Path"
# Get users from the "users-App" group
$members = Get-ADGroupMember -Identity "users-App" -Recursive | Get-ADUser -Properties sAMAccountName, displayName, Mail, homeDirectory, Department |
Select-Object sAMAccountName, displayName, Mail, homeDirectory, Department |
Sort-Object displayName
# Initialize an array to collect users with no data
$usersWithNoData = @()
# Email configuration
$recipients = "App-DailyReport@domain.local"
$sender = "Users-App@domain.local"
$subject = "App User Backup Report"
# Prepare HTML body
$htmlBody = @"
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
<table>
<tr>
<th>Display Name</th>
<th>Mail</th>
<th>Home Directory</th>
<th>Department</th>
<th>Allocated Size</th>
<th>Number of Files</th>
<th>Number of Folders</th>
</tr>
"@
# Loop through users and add data to HTML body
foreach ($member in $members) {
$trimmedSAM = $member.sAMAccountName.Trim()
$userData = $csvData | Where-Object { $_."Username".Trim() -eq $trimmedSAM }
# Check if user is found in CSV data
if ($userData) {
$displayName = $member.displayName
$mail = $member.Mail
$homeDirectory = $member.homeDirectory
$department = $member.Department
$allocatedSize = $userData."Allocated Size"
$numFiles = $userData."Number of Files"
$numFolders = $userData."Number of Folders"
$htmlBody += @"
<tr>
<td>$displayName</td>
<td>$mail</td>
<td>$homeDirectory</td>
<td>$department</td>
<td>$allocatedSize</td>
<td>$numFiles</td>
<td>$numFolders</td>
</tr>
"@
}
else {
# Collect users with no data in the array
$usersWithNoData += $trimmedSAM
}
}
# Close the HTML table
$htmlBody += "</table>"
# Add section for users with no data
$htmlBody += @"
<br>
<h3>Users with No Data:</h3>
<ul>
"@
foreach ($user in $usersWithNoData) {
$htmlBody += "<li>$user</li>"
}
$htmlBody += "</ul>"
# Send email
Send-MailMessage -To $recipients -From $sender -Subject $subject -Body $htmlBody -BodyAsHtml -SmtpServer "smtp.domain.local"