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

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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)
 
 [xml]$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 *