Hi,
There was a requirement to our customer i.e.
Every month email should be triggered to the customer with count of new document uploads of the preceding month.
Along with this customer requires the below details of additions as an attachment
For this, first we have created site columns as below
Below are settings of the view that has been created
"]
$fromaddress = "abc@domain.com"
$toaddress = " abc@domain.com "
$ccaddress = " abc@domain.com "
$auditloglib=$web.Lists[" AuditLogReports"]
$reportid=Get-Random
$context = Get-SPServiceContext $site
$date = get-date
$numdays = $date.Day
$ed = $date.AddDays(-$numdays)
$numdays = $ed.Day
$sd = $ed.AddDays( -$numdays + 1)
$sd = ((($sd.AddHours(0-$sd.hour)). AddMinutes(0-$sd.Minute)). AddSeconds(0-$sd.Second)). AddMilliseconds(0-$sd. millisecond)
$ed = $ed.AddDays(1)
$ed = ((($ed.AddHours(0-$ed.hour)). AddMinutes(0-$ed.Minute)). AddSeconds(0-$ed.Second)). AddMilliseconds(0-$ed. millisecond)
#EndRegion Global Variables
#Region Sales Workshop Additions
$view = $stlist.Views["CurrentMonth"]
$items = $stlist.GetItems($view)
$salestoolsadditions = 0
$salestoolsadditions = $items.Count
$Headers = 'FileName,Description, Modified,Created By,Modified By,Created'
$filename = "D:\NewAdditions-" + $sd.Date.Date.ToString("MMM") + $sd.Date.Date.ToString("yy")+$ reportid + ".csv"
Add-Content $filename $Headers
foreach($MItem in $items)
{
Write-Host $MItem["Name"]
Write-Host $MItem["Description"]
Write-Host $MItem["Modified"]
Write-Host $MItem["Created By"]
Write-Host $MItem["Modified By"]
Write-Host $MItem["Created"]
$DocDetails = $MItem["Name"]+","+$MItem[" Description"]+","+$MItem[" Modified"]+","+$MItem["Created By"].Split("#")[1] +","+$MItem["Modified By"].Split("#")[1]+","+$MItem[ "Created"]
Add-Content $filename $DocDetails
}
$files = Get-ChildItem -Path $filename -Force -Recurse
foreach ($salestoolsadditionsfile in $files)
{
$stream = $salestoolsadditionsfile. OpenRead()
$done= $auditloglib.RootFolder.Files. Add($salestoolsadditionsfile. Name, $stream, $true)
$stream.Close()
Write-Host $done.Name "Uploaded into the Document Library"$auditloglib -BackgroundColor Green
}
#EndRegion Sales Tools Additions
#############Email for New Additions ############################## ######
$Subject = "New Additions for the Month of -" + $sd.Date.Date.ToString("MMM")+ $sd.Date.Date.ToString("yy")
$body = "----------------------------- ------------------------------ ------------------------------ ---"<br>New Additions in Sales Tools are - "+$salestoolsadditions<br> ------------------------------ ------------------------------ ------------------------------ -------"
$attachment = $filename
#SMTP IP Address of Production
$smtpserver = "IP Address"
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.To.Add($ccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($ attachment)
$message.Attachments.Add($ attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($ smtpserver)
$smtp.Send($message)
$message.Dispose()
$smtp.Dispose()
$salestoolsadditionsfile. Delete()
############################## #########################
}
catch
{
Add-Content D:\Logs\ SalesToolsNewAdditionsLogRepor t.txt $DocNameError
}
############################## ######## Email for New Additions ############################## ############################## ###############
Finally the output is as below through attachment in the mail
No comments:
Post a Comment