Run a Console Application and Mail Output Report with PowerShell

We had a console application GetStorageReport which runs and generates detailed storage reports based on some SharePoint APIs. Got the requirement to run the tool periodically and mail the output report of the application. Scheduled a PowerShell script to execute the application and mail the report to Administration team.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#Trigger the executable file
Start-Process "D:\Scripts\StorageReport\GetStorageReport.exe"

# Wait until the report is generated
while(-not (Test-Path "D:\Scripts\StorageReport\StorageReport.csv"))
  {
    Start-Sleep 30  #sleep for 30 seconds
  }
  
$ReportDate=get-date -Format "MMM-dd-yyyy"

#Rename the Report 
Rename-Item -Path "D:\Scripts\StorageReport\StorageReport.csv" -NewName "D:\Scripts\StorageReport\StorageReport_$ReportDate.csv"
 
  #Send the Mail
 $MailMessageParameters = @{
    SmtpServer = "smtp.company.org"
    Subject = "SharePoint Storage Report - $ReportDate"
    Body = "Please find attached Database storage report as on $ReportDate"
    From = "SharePointReports@company.com"
    To = "AdminTeam@company.com"
    Attachments ="D:\Scripts\StorageReport\StorageReport_$ReportDate.csv"
}
 Send-MailMessage @MailMessageParameters
 
 #Remove the Report
 if (Test-Path "D:\Scripts\StorageReport\StorageReport_$ReportDate.csv")
 {
    Remove-Item "D:\Scripts\StorageReport\StorageReport_$ReportDate.csv"
 }

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *