{"id":124,"date":"2012-12-20T12:09:44","date_gmt":"2012-12-20T19:09:44","guid":{"rendered":"http:\/\/blog.gptnet.net\/?p=124"},"modified":"2012-12-27T08:38:48","modified_gmt":"2012-12-27T15:38:48","slug":"124","status":"publish","type":"post","link":"http:\/\/blog.gptnet.net\/?p=124","title":{"rendered":"XML and Powershell"},"content":{"rendered":"<p>Here&#8217;s a sample script which shows how to work with XML files in Powershell and much more.<\/p>\n<p>Logic of this script:<\/p>\n<p>Check if folder has more than 2 files<br \/>\nIf it finds more than 2 files send email via function SendMail()<br \/>\nNext read first XML file<br \/>\nif 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.<br \/>\nif 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.<\/p>\n<p>Script<!--more--><\/p>\n<pre lang=\"PowerShell\" line=\"1\"># Created: Naz Snidanko\r\n# Date: Aug 22, 2012\r\n# Description: This script checks wheeltime folder and if it sees more than 2 XML files it run's error correction\r\n#              error correction removes special characters from complaint and complain fields and replaces them with space.\r\n#              It also send email notification to specified email.\r\n\r\n#function for an email notification\r\n function sendMail{\r\n\r\n     Write-Host \"Sending Email\"\r\n\r\n     #SMTP server name\r\n     $smtpServer = \"server\"\r\n\r\n     #Creating a Mail object\r\n     $msg = new-object Net.Mail.MailMessage\r\n\r\n     #Creating SMTP server object\r\n     $smtp = new-object Net.Mail.SmtpClient($smtpServer)\r\n\r\n     #Email structure \r\n     $msg.From = \"noreply@aa.com\"\r\n     $msg.ReplyTo = \"noreply@aa.com\"\r\n     $msg.To.Add(\"nsnidanko@aa.com\")\r\n     $msg.subject = \"Invalid characters\"\r\n     $msg.body = \"This is automatic email generated by a script.  Thank you, Magic Script.\"\r\n\r\n     #Sending email \r\n     $smtp.Send($msg)\r\n  }\r\n\r\n# Working folder\r\n$path = \"C:\\out\"\r\n\r\n# count number of files inside the folder\r\n[int]$intFileNumber = [IO.Directory]::GetFiles($path).Count\r\n\r\n# execute script only if there is more than 2 files inside the folder = stuck\r\nif ($intFileNumber -gt 2){\r\n\r\n#Sending email notification\r\nsendMail\r\n\r\ncd $path\r\n# variables\r\n\r\n Get-ChildItem -path $path| ForEach-Object {\r\n\r\n $file = Get-ChildItem ($_.FullName)\r\n\r\n $xmlInpt = Get-Content $file\r\n\r\n    # count orders in XML file\r\n    [int]$intTotal = $xmlInpt.Envelope.Body.repairorders.\"repair-order\".Count\r\n    [int]$intCnt = 0\r\n    echo $intTotal\r\n\r\n       # Note: Powershell doesn't read XML field as an array if only 1 present. If statement to verify if array should be used  \r\n       if ($intTotal -ge 2) {\r\n                do\r\n                {\r\n\r\n                    # data input\r\n                    [string]$strCompl = $xmlInpt.Envelope.Body.repairorders.\"repair-order\"[$intCnt].\"detail-records\".\"detail-record\".complaint\r\n                    [string]$strCause = $xmlInpt.Envelope.Body.repairorders.\"repair-order\"[$intCnt].\"detail-records\".\"detail-record\".cause\r\n\r\n                    # Validate for special character and replace with blank space\r\n\r\n                    [string]$strComplFix = [System.Text.RegularExpressions.Regex]::Replace($strCompl,\"[^1-9a-zA-Z_]\",\" \");\r\n                    [string]$strCauseFix = [System.Text.RegularExpressions.Regex]::Replace($strCause,\"[^1-9a-zA-Z_]\",\" \");\r\n\r\n                    # replace values in file\r\n                    $xmlInpt.Envelope.Body.repairorders.\"repair-order\"[$intCnt].\"detail-records\".\"detail-record\".complaint = $strComplFix\r\n                    $xmlInpt.Envelope.Body.repairorders.\"repair-order\"[$intCnt].\"detail-records\".\"detail-record\".cause = $strCauseFix\r\n\r\n                    # increase counter (number of work orders inside file)\r\n                    #echo $intCnt\r\n                    $intCnt++\r\n                    #echo \"Inside loop:\" $file.FullName\r\n                } while ($intCnt -lt $intTotal)\r\n\r\n        #save validated file\r\n        $xmlInpt.Save(\"$file\") \r\n        }\r\n        else {\r\n                    # data input\r\n                    [string]$strCompl = $xmlInpt.Envelope.Body.repairorders.\"repair-order\".\"detail-records\".\"detail-record\".complaint\r\n                    [string]$strCause = $xmlInpt.Envelope.Body.repairorders.\"repair-order\".\"detail-records\".\"detail-record\".cause\r\n\r\n                    # Validate for special character and replace with blank space\r\n\r\n                    [string]$strComplFix = [System.Text.RegularExpressions.Regex]::Replace($strCompl,\"[^1-9a-zA-Z_]\",\" \");\r\n                    [string]$strCauseFix = [System.Text.RegularExpressions.Regex]::Replace($strCause,\"[^1-9a-zA-Z_]\",\" \");\r\n\r\n                    # replace values in file\r\n                    $xmlInpt.Envelope.Body.repairorders.\"repair-order\".\"detail-records\".\"detail-record\".complaint = $strComplFix\r\n                    $xmlInpt.Envelope.Body.repairorders.\"repair-order\".\"detail-records\".\"detail-record\".cause = $strCauseFix\r\n        #save validated file\r\n        $xmlInpt.Save(\"$file\")\r\n        }\r\n    } \r\n\r\n}\r\n\r\n#else for test\r\n# {\r\n# echo \"No backlog, yay!\"\r\n# }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;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 &hellip; <a href=\"http:\/\/blog.gptnet.net\/?p=124\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[45,48,47,46],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell-2","tag-regex","tag-smtp","tag-xml-powershell"],"_links":{"self":[{"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=\/wp\/v2\/posts\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=124"}],"version-history":[{"count":5,"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":128,"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=\/wp\/v2\/posts\/124\/revisions\/128"}],"wp:attachment":[{"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.gptnet.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}