XML and Powershell

Here’s a sample script which shows how to work with XML files in Powershell and much more.

Logic of this script:

Check if folder has more than 2 files
If it finds more than 2 files send email via function SendMail()
Next read first XML file
if it finds multiple fields under Envelope.Body.repairorders, use array for each object. Next it replaces characters in this XML object using regex and save it.
if it finds single field under Envelope.Body.repairorders, do not use array as Powershell fails to recognize XML array object 0 in such cases, thus i had to make this extra step. It does the same magic.

Script

# Created: Naz Snidanko
# Date: Aug 22, 2012
# Description: This script checks wheeltime folder and if it sees more than 2 XML files it run's error correction
#              error correction removes special characters from complaint and complain fields and replaces them with space.
#              It also send email notification to specified email.

#function for an email notification
 function sendMail{

     Write-Host "Sending Email"

     #SMTP server name
     $smtpServer = "server"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure 
     $msg.From = "noreply@aa.com"
     $msg.ReplyTo = "noreply@aa.com"
     $msg.To.Add("nsnidanko@aa.com")
     $msg.subject = "Invalid characters"
     $msg.body = "This is automatic email generated by a script.  Thank you, Magic Script."

     #Sending email 
     $smtp.Send($msg)
  }

# Working folder
$path = "C:\out"

# count number of files inside the folder
[int]$intFileNumber = [IO.Directory]::GetFiles($path).Count

# execute script only if there is more than 2 files inside the folder = stuck
if ($intFileNumber -gt 2){

#Sending email notification
sendMail

cd $path
# variables

 Get-ChildItem -path $path| ForEach-Object {

 $file = Get-ChildItem ($_.FullName)

 $xmlInpt = Get-Content $file

    # count orders in XML file
    [int]$intTotal = $xmlInpt.Envelope.Body.repairorders."repair-order".Count
    [int]$intCnt = 0
    echo $intTotal

       # Note: Powershell doesn't read XML field as an array if only 1 present. If statement to verify if array should be used  
       if ($intTotal -ge 2) {
                do
                {

                    # data input
                    [string]$strCompl = $xmlInpt.Envelope.Body.repairorders."repair-order"[$intCnt]."detail-records"."detail-record".complaint
                    [string]$strCause = $xmlInpt.Envelope.Body.repairorders."repair-order"[$intCnt]."detail-records"."detail-record".cause

                    # Validate for special character and replace with blank space

                    [string]$strComplFix = [System.Text.RegularExpressions.Regex]::Replace($strCompl,"[^1-9a-zA-Z_]"," ");
                    [string]$strCauseFix = [System.Text.RegularExpressions.Regex]::Replace($strCause,"[^1-9a-zA-Z_]"," ");

                    # replace values in file
                    $xmlInpt.Envelope.Body.repairorders."repair-order"[$intCnt]."detail-records"."detail-record".complaint = $strComplFix
                    $xmlInpt.Envelope.Body.repairorders."repair-order"[$intCnt]."detail-records"."detail-record".cause = $strCauseFix

                    # increase counter (number of work orders inside file)
                    #echo $intCnt
                    $intCnt++
                    #echo "Inside loop:" $file.FullName
                } while ($intCnt -lt $intTotal)

        #save validated file
        $xmlInpt.Save("$file") 
        }
        else {
                    # data input
                    [string]$strCompl = $xmlInpt.Envelope.Body.repairorders."repair-order"."detail-records"."detail-record".complaint
                    [string]$strCause = $xmlInpt.Envelope.Body.repairorders."repair-order"."detail-records"."detail-record".cause

                    # Validate for special character and replace with blank space

                    [string]$strComplFix = [System.Text.RegularExpressions.Regex]::Replace($strCompl,"[^1-9a-zA-Z_]"," ");
                    [string]$strCauseFix = [System.Text.RegularExpressions.Regex]::Replace($strCause,"[^1-9a-zA-Z_]"," ");

                    # replace values in file
                    $xmlInpt.Envelope.Body.repairorders."repair-order"."detail-records"."detail-record".complaint = $strComplFix
                    $xmlInpt.Envelope.Body.repairorders."repair-order"."detail-records"."detail-record".cause = $strCauseFix
        #save validated file
        $xmlInpt.Save("$file")
        }
    } 

}

#else for test
# {
# echo "No backlog, yay!"
# }
This entry was posted in Powershell and tagged , , , . Bookmark the permalink.

Leave a Reply

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