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.

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()

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax