Trying to write a script to output a list of users who currently have a specific file open for use. I have the results showing proper when executed in powershell but when I read them into a string to send out in an email they all come out on a single line. I need to figure out how to get the usernames to show on multiple lines and not all in one line. I also need to figure out how to export the results as a file and attach to the same email. Any help you can offer would be great!
Get-SmbOpenFile | Where-Object -Property ShareRelativePath -Match "S2K" | Where-Object -Property ShareRelativePath -NotMatch "IMAGES" | Sort-Object ClientUsername | Select-Object -ExpandProperty ClientUsername
$body = C:\Results.ps1
Send-MailMessage -To (To address) -from (From address) -subject "Currently logged in users for $(Get-Date)" -Body "The below list of users are using the file.`n`nAttached is a more user friendly version.`n`n$body" -SmtpServer (SMTP Server)
Currently outputs like this:
Desiring it output like this: