Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PDF Report Check Script (Created by wbzhe1)
- # It uses itextsharp.dll to parse PDF files, it can be downloaded from SourceForge
- Add-Type -Path .\itextsharp.dll
- $validDate = "11 Dec 2015"
- $day = ([datetime]$validDate).ToString('dd') ; $day = $day -replace "^0", ""
- #Define valid Month+Year format here, this will combine with $day in the search
- $validMYArray = @(([datetime]$validDate).ToString(' MMM yyyy'))
- $validMYArray += ([datetime]$validDate).ToString(' MMMM yyyy')
- $validMYArray += ([datetime]$validDate).ToString('/MM/yyyy')
- $validMYArray += ([datetime]$validDate).ToString('-MMM-yy')
- # PDF Counters
- $countTotal = $countGood = $countBad = 0
- $badDate = $foundDate = ""
- $PDFPath="\\server\share"
- $badPDF="Bad PDF (e.g. incorrect date, empty date):`n`n"
- Write-Host "Mapping a Drive..."
- New-PSDrive -Name UNCReportDrive -PSProvider FileSystem -Root $PDFPath
- Write-Host "Parsing PDF Files..."
- #################################################################
- ### Search PDF Reports from UNC folder, parse each PDF ###
- ### output PDFs with incorrect date ###
- #################################################################
- Get-ChildItem -Path UNCReportDrive:\ -Filter *.pdf -Recurse |
- Foreach-Object{
- $countTotal++
- $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $_.FullName
- $pageText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader, 1) -join "" -split "`n"
- # search each line, look for a date which match the format defined in above
- :loop ForEach ($line in $pageText) {
- ForEach ($validMY in $validMYArray) {
- if ($line -cmatch "0?$day$validMY") {
- $countGood++
- $foundDate="Yes"
- break loop
- } elseif ($line -cmatch "[0-3]?\d$validMY" -and $badDate -eq "") {
- # extract incorrect date and append it to the PDFs
- $badDate = [regex]::Matches($line, "([0-3]?\d$validMY)")[0].Groups[1].Value
- break loop
- }
- }
- }
- if ($foundDate -ne "Yes") {
- $countBad++
- $badPDF += $_.FullName + " ($badDate)`t`n" #<-insert a Tab character before `n to avoid Outlook Extra Line Break issue
- $badDate = ""
- }
- $foundDate = ""
- }
- $reader.Dispose() #<-destroy/free the Object, it locks the PDF reports otherwise
- # Unmap the drive
- Remove-PSDrive -Name UNCReportDrive
- $stopWatch.Stop()
- # Remove "\\server\share\" from file path
- $badPDF = $badPDF -replace "\\\\.*\d\\", ""
- Write-Host "$badPDF`n`n$countTotal PDFs, Good=$countGood, Bad=$countBad $($stopWatch.Elapsed.TotalSeconds) sec
- $PDFPath$validDateNum"
- Write-Host "Email Result..."
- $messageParameters = @{
- Subject = "PDF Checked has finish"
- Body = "Say something here"
- From = "a@yahoo.com"
- To = "b@yahoo.com"
- SmtpServer = "mailserver"
- }
- Send-MailMessage @messageParameters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement