79171033

Date: 2024-11-08 17:38:01
Score: 1.5
Natty:
Report link
Better example of using HTMLemail formatting 

``######################################################################### ## PowerShell Script: # #########################################################################

#Global Variable Section
#########################################
#Email Variables
$emailTo = "test.org"
$emailFrom = "test.org"
$smtpServer = "Mail.test.org" 
$message = ""
$subject = "J-Summary"
$message = ""

#Default Summary Variables; Set to Zero/Blank
$totalDollar = 0
$totalAccptDollar = 0
$numTrans = 0
$numAccptTrans = 0
$batchNum = ""
$transDetail = ""

#HTML Header Variables
$Header1 = "J "
$Header2 = "EC OPERATIONS"
$Header3 = " ALIDATION STATUS REPORT"

#File Path Section
#########################################
# Specify the path to EDI 820/824/997 Files
$filePath820 = "C:\Users\test\Desktop\Chase-Summary_Files\JPM820.outb*"
$filePath824 = "C:\Users\test\Desktop\Chase-Summary_Files\Chase_SH_AP_824_*"
$filePath997 = "C:\Users\test\Desktop\Chase-Summary_Files\Chase_SH_AP_997_*"
$archiveFolder = "C:\Users\test\Desktop\Chase-Summary_Files\_Archive\"

# Read the content of the Summary file
$content820 = Get-Content -Path $filePath820 -Raw
$content824 = Get-Content -Path $filePath824 -Raw
$content997 = Get-Content -Path $filePath997 -Raw

#Remove Line Feeds from EDI File Used For Easier Processing/Parsing Logic
$content820 = [string]::join("",($content820.split("`n")))
$content824 = [string]::join("",($content824.split("`n")))
$content997 = [string]::join("",($content997.split("`n")))

#HTML Compiler Section
#########################################
#Build Header HTML Section
$rptHeader = @"
<html>
<body>
<center><strong>$($Header1)</strong></center>
<center><strong>$($Header2)</strong></center>
<center><strong>$($Header3)</strong></center>
<br>
"@

#Build Footer HTML Section
$rptFooter = @"
</table>
<br>
<br>
STATUS(ST): TA=ACCEPTED TC=ACCEPTED W/CHANGE TE=ACCEPTED W/ERROR TR=REJECTED
<br>
<br>
<br>
IF YOU HAVE ANY QUESTIONS, PLEASE OPEN A SERVICENOW INCIDENT
ASSIGNED TO <strong>APP-BUSINESS-MATERIALS MANAGEMENT</strong>
<br>
<center>*****END OF REPORT*****</center>
</body>
</html>
"@

#EDI Reader Section to Finalize HTML Compiler
#########################################
#Read EDI 820 File (Used to Gather Total Received Number of Transactions and Dollar Amount)
$ediSegments = $content820 -split "\\"
##Parse Through Fields of Section and Get Total Dollar Amount Sent For All Transactions Regardless of Status For Summary Line
for ($s = 0; $s -lt $ediSegments.Count; $s++) {
    $ediSummarySegment = $ediSegments[$s] -split "\*"
    
    #Calculate Total Dollar Amount By Collecting Amount From Each Read BPR Section
    if ($ediSummarySegment[0] -eq "BPR") {
        $totalDollar = $totalDollar + $ediSummarySegment[2]
    }
    #Collect Total Number of Transactions From the GE Section
    elseif ($ediSummarySegment[0] -eq "GE") {
        $numTrans = $ediSummarySegment[1]
    }
}

#Read EDI 824 File (Used to Gather Total Processed Number of Transactions and Dollar Amount)
$ediSegments = $content824 -split "\\"
##Parse Through Fields of Section and Get Total Number of Accepted Dollar Amount and Accepted Transactions
for ($e = 0; $e -lt $ediSegments.Count; $e++) {
    $ediSummarySegment = $ediSegments[$e] -split "\*"

    #Calculate Total Dollar Amount By Collecting Amount From Each Read AMT Section
    if ($ediSummarySegment[0] -eq "AMT") {
        $totalAccptDollar = $totalAccptDollar + $ediSummarySegment[2]
    }
    #Collect Total Number of Transactions From the GE Section
    elseif ($ediSummarySegment[0] -eq "GE") {
        $numAccptTrans = $ediSummarySegment[1]
    }
}

#Parse Through Fields of Section
for ($i = 0; $i -lt $ediSegments.Count; $i++) {
    $ediSegment = $ediSegments[$i] -split "\*"

    #Collect and Format Date Value From the ISA Section
    if ($ediSegment[0] -eq "ISA") {
        $Customer = $ediSegment[8].TrimEnd()
        $Date = $ediSegment[9]
        $FormatDate = "$($Date.Substring(2,2))/$($Date.Substring(4,2))/$($Date.Substring(0,2))"
        $Time = $ediSegment[10]
        $FormatTime = "$($Time.Substring(0,2)):$($Time.Substring(2,2))"

        #Create Report Info Table
        $rptInfo = @"
<table border="0">
<tr><td>Customer ID: $($Customer)</td><td>&nbsp;</td><td>$($FormatDate) $($FormatTime) PT</td></tr>
</table>
<br>
"@
    }
    elseif ($ediSegment[0] -eq "ST") {
        $ediType = $ediSegment[0] -split "\*"    
    }
    elseif ($ediSegment[0] -eq "BGN") {
        #Collect Batch Number and Build Summary Table Section Based on First Collected Occurrence; Ignore All Other Values As They Would Be Duplicating This Section
        if ($batchNum -eq "") {
            $batchNum = $ediSegment[2]
                
            #Create Summary Table For First Time - Include Table Header
            $rptSummary = @"
<table border="1">
<tr><th>FILE# / BATCH#</th><th>AMOUNT</th><th># TRANS</th><th>STATUS</th></tr>
<tr><td>$($batchNum)</td><td>$($totalDollar)</td><td>$($numTrans)</td><td>TRANS RECEIVED</td></tr>
<tr><td>$($batchNum)</td><td>$($totalAccptDollar)</td><td>$($numAccptTrans)</td><td>TRANS ACCEPTED</td></tr>
</table>
<br>
"@
        }
    }
    elseif ($ediSegment[0] -eq "OTI") {
        #Collect and Format Date Value
        $effDate = $ediSegment[6]
        $effFormatDate = "$($effDate.Substring(4,2))/$($effDate.Substring(6,2))/$($effDate.Substring(0,4))"

        #Collect Transaction Detail and Build Detail Table and Rows Based on Each OTI Section Found in EDI File
        if ($transDetail -eq "") {
            #Create Transaction Detail Table For First Time - Include Table Header
            $transDetail = @"
<table border="1">
<tr><th>ST</th><th>TRANS #</th><th>TRACE #</th><th>EFF DATE</th><th>AMOUNT</th><th>MESSAGE</th></tr>
<tr><td>$($ediSegment[1])</td><td>$($ediSegment[9])</td><td>$($ediSegment[3])</td><td>$($effFormatDate)</td>
"@
        }
        #Build Additional Table Rows
        else {
        $transDetail = $transDetail + 
 @"
<tr><td>$($ediSegment[1])</td><td>$($ediSegment[9])</td><td>$($ediSegment[3])</td><td>$($effFormatDate)</td>
"@
        }
    }
    #Append Amount Value as Last Column in Row
    elseif ($ediSegment[0] -eq "AMT") {
        if ($transDetail -ne "") {
            $transDetail = $transDetail + "<td>$($ediSegment[2])</td><td></td></tr>" + "`r`n"
        }
    }
}

#Reset Variables to Avoid Duplication of Displayed Data
$errDetail = ""
$errReport = ""

#Read 997 EDI File (Used to Gather Total Number of Errored Transactions and Dollar Amounts)
$ediSegments = $content997 -split "\\"

#Parse Through Fields of Section
for ($i = 0; $i -lt $ediSegments.Count; $i++) {
    $ediSegment = $ediSegments[$i] -split "\*"
    
    #Collect Transaction Error Detail and Build Detail Table and Rows Based on Each AK2 Section Found in EDI File
    if ($ediSegment[0] -eq "AK2") {
        if ($errDetail -eq "") {
            #Create Transaction Error Detail Table For First Time - Include Table Header
            $errDetail = @"
<table border="1">
<tr><th>TRANS #</th><th>MESSAGE</th></tr>
<tr><td style="color:red"><strong>$($ediSegment[2])</strong></td><td style="color:red"><strong>TRANSACTION NOT ACCEPTED</strong></td></tr> `r`n
"@
        }
        #Build Additional Table Rows
        else {
            $errDetail = $errDetail + 
 @"
<tr><td style="color:red"><strong>$($ediSegment[2])</strong></td><td style="color:red"><strong>TRANSACTION NOT ACCEPTED</strong></td></tr> `r`n
"@
        }
    }
}

#Check If Error Detail Data Exists
if ($errDetail -eq "") {
    #Display Default Message to Customer Alerting No Errors Exist
    $errReport = @"
<br>
No Errors Exist.
<br>
"@
}
else {
    #Append Built Transaction Error Table to End Table Tag
    $errReport = @"
$($errDetail)
</table>
<br>
"@
}

#Build Email Message Body Section
#########################################
#Compile HTML Code
$body = @"
$($rptHeader)
$($rptInfo)
$($rptSummary)
$($errReport)
$($transDetail)
$($rptFooter)
"@

#Build Email Notification Section
#########################################
$message = $body
$anonUsername = "anonymous"
$anonPassword = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
$anonCredentials = New-Object System.Management.Automation.PSCredential($anonUsername,$anonPassword)
Send-MailMessage -smtpserver "$smtpServer" -from "$emailFrom" -to "$emailTo" -subject "$subject" -body "$message" -BodyAsHtml -credential $anonCredentials 

#Cleanup EDI File Section
#########################################
#Move Files to Archive Directory
Move-Item -Path $filePath820 -Destination $archiveFolder
Move-Item -Path $filePath824 -Destination $archiveFolder
Move-Item -Path $filePath997 -Destination $archiveFolder
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • No latin characters (2):
  • Filler text (0.5): #########################################################################
  • Filler text (0): #########################################################################
  • Low reputation (0.5):
Posted by: Configueroa