Saturday, August 29, 2015

PowerShell Script To Get Documents Last Updated Details


Hi,

We had requirement to get the below details of all the files in a document library of last month as report.
This report needs to be sent to customer as an attachment through email.


For this we have written the PowerShell script as below

if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.SharePoint.Powershell
}
#Global Variables
$site=Get-SPSite "siteurl"
$web=get-SPWeb "weburl"
$list=$web.Lists["doclibname"]
$fromaddress = "abc@domain.com"
$toaddress = " def@domain.com "
$ccaddress = "xyz@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 Last Updated Data
try
   {
   
    

    $Headers = 'FileName,Description,Modified,Created By,Modified By,Path,Created'

    $filename = "D:\filename-" + $sd.Date.Date.ToString("MMM") + $sd.Date.Date.ToString("yy")+$reportid + ".csv"

   


    Add-Content $filename $Headers
    $Items = $list.items
    Write-Host  "Item Count" + $Items.Count

               

    foreach($MItem in $Items)   
    
    {
        Write-Host $Items.Count
        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["Path"]
        Write-Host $MItem["Created"]       
        
        $DocDetails = $MItem["Name"]+","+$MItem["Description"]+","+$MItem["Modified"]+","+$MItem["Created By"].Split("#")[1] +","+$MItem["Modified By"].Split("#")[1]+","+$MItem["Path"]+","+$MItem["Created"]       
        Add-Content $filename $DocDetails
    }
    $files = Get-ChildItem -Path $filename -Force -Recurse
foreach ($file in $files)
        {
          
          $stream = $file.OpenRead()

          $done= $auditloglib.RootFolder.Files.Add($file.Name, $stream, $true)

          $stream.Close()
         
          Write-Host $done.Name  "Uploaded into the Document Library"$auditloglib -BackgroundColor Green   
               

        }
Write-Host "Last Updated Report Uploaded successfully into"+$auditloglib
#############Email for Last Updated Data####################################
$Subject = "Sales Tools Last Updated-" + $sd.Date.Date.ToString("MMM")+$sd.Date.Date.ToString("yy")
$body = "Hi, Please find attached Last Updated report for the Month :" + $sd.Date.Date.ToString("MMM") +$sd.Date.Date.ToString("yy")
$attachment = $filename
#SMTP IP Address of Production
$smtpserver = "0.0.0.0"
$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()
$file.Delete()
#######################################################    
  }
  catch
    {
  
   Add-Content D:\Logs\LastUpdatedLogReport.txt $DocNameError
    }
#EndRegion Last Updated Data

Post generating the report through csv file.

We are uploading the same to document library and

We are deleting the same from the physical path through Power Shell script


No comments:

Post a Comment