Advertisement
Guest User

Untitled

a guest
Dec 30th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. # PDF Report Check Script (Created by wbzhe1)
  2. # It uses itextsharp.dll to parse PDF files, it can be downloaded from SourceForge
  3.  
  4. Add-Type -Path .\itextsharp.dll
  5. $validDate = "11 Dec 2015"
  6.  
  7. $day = ([datetime]$validDate).ToString('dd') ; $day = $day -replace "^0", ""
  8. #Define valid Month+Year format here, this will combine with $day in the search
  9. $validMYArray = @(([datetime]$validDate).ToString(' MMM yyyy'))
  10. $validMYArray += ([datetime]$validDate).ToString(' MMMM yyyy')
  11. $validMYArray += ([datetime]$validDate).ToString('/MM/yyyy')
  12. $validMYArray += ([datetime]$validDate).ToString('-MMM-yy')
  13.  
  14. # PDF Counters
  15. $countTotal = $countGood = $countBad = 0
  16. $badDate = $foundDate = ""
  17. $PDFPath="\\server\share"
  18. $badPDF="Bad PDF (e.g. incorrect date, empty date):`n`n"
  19.  
  20. Write-Host "Mapping a Drive..."
  21. New-PSDrive -Name UNCReportDrive -PSProvider FileSystem -Root $PDFPath
  22.  
  23. Write-Host "Parsing PDF Files..."
  24. #################################################################
  25. ### Search PDF Reports from UNC folder, parse each PDF ###
  26. ### output PDFs with incorrect date ###
  27. #################################################################
  28. Get-ChildItem -Path UNCReportDrive:\ -Filter *.pdf -Recurse |
  29. Foreach-Object{
  30. $countTotal++
  31. $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $_.FullName
  32. $pageText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader, 1) -join "" -split "`n"
  33.  
  34. # search each line, look for a date which match the format defined in above
  35. :loop ForEach ($line in $pageText) {
  36. ForEach ($validMY in $validMYArray) {
  37. if ($line -cmatch "0?$day$validMY") {
  38. $countGood++
  39. $foundDate="Yes"
  40. break loop
  41. } elseif ($line -cmatch "[0-3]?\d$validMY" -and $badDate -eq "") {
  42. # extract incorrect date and append it to the PDFs
  43. $badDate = [regex]::Matches($line, "([0-3]?\d$validMY)")[0].Groups[1].Value
  44. break loop
  45. }
  46. }
  47. }
  48. if ($foundDate -ne "Yes") {
  49. $countBad++
  50. $badPDF += $_.FullName + " ($badDate)`t`n" #<-insert a Tab character before `n to avoid Outlook Extra Line Break issue
  51. $badDate = ""
  52. }
  53. $foundDate = ""
  54. }
  55. $reader.Dispose() #<-destroy/free the Object, it locks the PDF reports otherwise
  56.  
  57. # Unmap the drive
  58. Remove-PSDrive -Name UNCReportDrive
  59. $stopWatch.Stop()
  60.  
  61. # Remove "\\server\share\" from file path
  62. $badPDF = $badPDF -replace "\\\\.*\d\\", ""
  63. Write-Host "$badPDF`n`n$countTotal PDFs, Good=$countGood, Bad=$countBad $($stopWatch.Elapsed.TotalSeconds) sec
  64. $PDFPath$validDateNum"
  65.  
  66. Write-Host "Email Result..."
  67. $messageParameters = @{
  68. Subject = "PDF Checked has finish"
  69. Body = "Say something here"
  70. From = "a@yahoo.com"
  71. To = "b@yahoo.com"
  72. SmtpServer = "mailserver"
  73. }
  74. Send-MailMessage @messageParameters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement