Today I ended up working on a task to convert some of my .NET code into more of a scriptable powershell form. I took the interop “itextsharp.dll” below and used the following code to generate a PDF will the fields filled in. The PDF was created using Acrobat pro and I added the functionality to Sign the Document with a certificate (Which also locks the document) so we can verify who signed off on the termination procedure. Once the user is Terminated they will show in a text feed which this script will parse and generated a PDF for the owner to fill out that such work has been executed and complied. They then sign the PDF and file with Human resources showing the user’s access was correctly decommissioned.
You may get a copy of ITextSharp here
In order to obtain a Adobe’s Signer’s cert to sign the PDF with, Follow these steps here.
And the signing of the form, I currently don’t have an signature set but once it is it will print your scanned signature in onto the document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host Add-Type -Path 'C:\itextsharp.dll' foreach($line in get-content "\\MyServer\c$\Feed.txt") { try { $recipients = $line -split [RegEx]::Escape("|") $samAccountName = $recipients[1].trim() $DeptCode = $recipients[2].trim() $JobCode = $recipients[3].trim() $FirstName = $recipients[4].trim() $MiddleInital = $recipients[5].trim() $LastName = $recipients[6].trim() $Director = $recipients[7].trim() $Hiredate = $recipients[8].trim() $Termed = $recipients[9].trim() $fullname = $FirstName if ($MiddleInital -ne "") { $fullname += " $MiddleInital" } $fullname += " $LastName" $TodaysDate = Get-Date -UFormat "%Y%m%d" #$User = Get-ADUser -LDAPFilter "(sAMAccountName=$samAccountName)" | Select-Object -Property samaccountname,enabled #If ($User -ne $Null) #{ if ($TodaysDate -eq $Termed) { Write-Host $samAccountName.PadRight(10) "`t" $fullname.ToString().PadRight(25) "`t" $Hiredate $Termed $OutputFile = 'C:\AutoTerm\' + $samAccountName + ".pdf" #The function will add to the PDF to create the complete filename $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList 'C:\term_checklist_template.pdf' $stamper = New-Object iTextSharp.text.pdf.PdfStamper($reader, [System.IO.File]::Create($OutputFile)) $stamper.AcroFields.SetField('DATEPRINTED', '') #This is auto populated. $stamper.AcroFields.SetField('DATETERMED', $Termed) $stamper.AcroFields.SetField('ENAME', $fullname) $stamper.AcroFields.SetField('EID', $samAccountName) foreach($line in get-content "\\MyServer\current deptcenter jobcodeDesc.txt") { $Lines = $line -split [RegEx]::Escape("|") $CurrentDeptCode = $Lines[1].trim() $CurrentDeptDesc = $Lines[2].trim() $CurrentJobCode = $Lines[3].trim() $CurrentJobDesc = $Lines[4].trim() if (($CurrentDeptCode -like $DeptCode) -and ($CurrentJobCode -like $JobCode)) { Write-Host $CurrentDeptDesc $CurrentJobDesc break } } $stamper.AcroFields.SetField('ETITLE', $CurrentJobDesc) $stamper.AcroFields.SetField('EDEPARTMENT', $CurrentDeptDesc) $stamper.Close() Send-MailMessage -From "Termination <PowershellPreProcessor@Domain.Com>" -To "Deprovisioning Team <DePro@Domain.Com>", "Deprovisioning Director <Manager@Domain.com>" -Subject "Terminated Report Checklist" -Body "Please fill out and send to HR" -Attachments $OutputFile -Priority High -dno onSuccess, onFailure -SmtpServer "myexchangeserver.info.sys" } #} } catch { } } for ($page = 1; $page -le $reader.NumberOfPages; $page++) { # extract a page and split it into lines $text = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader,$page).Split([char]0x000A) Write-Host "Page $($page) contains $($text.Length) lines. This is line 5:" Write-Host $text[4] #foreach ($line in $text) #{ # any tasks #} } $reader.Close() |