Sunday, July 1, 2012

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.

Here is my PowerShell code:
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 = ""
    Subject = "SharePoint Storage Report - $ReportDate"
    Body = "Please find attached Database storage report as on $ReportDate"
    From = ""
    To = ""
    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"

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...