Search and get content from data in XML and then place that value in another tag using powershell - powershell

Sample text file contains:
`<?xml version="1.0" encoding="UTF-8" ?>
<Document xmlns ="urn:iso:std:iso:20022:tech:xsd:camt.056.001.01"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FIToFIPmtCxlReq>
<Assgnmt>
<Id>ID123456</Id>
<Assgnr>
<Agt>
<FinInstnId>
<BIC>BICSEND</BIC>
</FinInstnId>
</Agt>
</Assgnr>
<Assgne>
<Agt>
<FinInstnId>
<BIC>BICRCV</BIC>
</FinInstnId>
</Agt>
</Assgne>
<CreDtTm>2020-12-16T09:05:15.0Z</CreDtTm>
</Assgnmt>
<CtrlData>
<NbOfTxs>1</NbOfTxs>
<CtrlSum>0</CtrlSum>
</CtrlData>
<Undrlyg>
<TxInf>
<CxlId>20201216.105.19344855940590400</CxlId>
<OrgnlGrpInf>
<OrgnlMsgId>REF123456789</OrgnlMsgId>
<OrgnlMsgNmId>pacs.008</OrgnlMsgNmId>
</OrgnlGrpInf>
<OrgnlInstrId>FT123456</OrgnlInstrId>
<OrgnlEndToEndId>NOTPROVIDED</OrgnlEndToEndId>
<OrgnlTxId>20201216.100.02202020</OrgnlTxId>
<OrgnlIntrBkSttlmAmt Ccy="EUR">25.23</OrgnlIntrBkSttlmAmt>
<OrgnlIntrBkSttlmDt>2020-12-16</OrgnlIntrBkSttlmDt>`
Please be informed that I would like to code PowerShell to extract the data in tag <OrgnlIntrBkSttlmAmt> (please note that the data length can change since this is an amount field) and then replace the "0" in tag <CtrlSum> with "25.23".
Can someone help me with this.
Thank you for your time.

The xml you show us is invalid as it is missing the following closing tags:
</TxInf>
</Undrlyg>
</FIToFIPmtCxlReq>
</Document>
If I add these, you could do this to update the value in the <CtrlSum> tag:
# load the xml from file
[xml]$xml = Get-Content -Path 'D:\Test\test.xml' -Raw
# get the amount from the 'OrgnlIntrBkSttlmAmt' tag
$amount = $xml.Document.FIToFIPmtCxlReq.Undrlyg.TxInf.OrgnlIntrBkSttlmAmt.'#text'
# use that amount to put in the 'CtrlSum' tag
$xml.Document.FIToFIPmtCxlReq.CtrlData.CtrlSum = $amount
# save the updated xml to file
$xml.Save('D:\Test\test.xml')

Related

pass json as parameter in power shell script

I have made a function for creating new xml node.there are two parameters in my function on is a existing xml file reference and second one is element value.while running the script its showing an error
code
function createProviderNode($xmlData,$propertyValue){
Write-Host 'inside createProviderNode'
Write-Host ($propertyValue)
#[xml]$xmlData = get-content E:\powershell\data.xml
$newProviderNode = $xmlData.CreateNode("element","provider","")
$newProviderNode.SetAttribute("name",$propertyValue)
$xmlData.SelectSingleNode('providers').AppendChild($newProviderNode)
$xmlData.save("E:\powershell\data.xml")
}
did i miss anything in this code?
The error message implies that while you expected $xmlData to contain an object of type [xml] (System.Xml.XmlDocument) - i.e., an XML document - in reality it was a string ([string]).
In other words: When you called your createProviderNode function, the 1st argument you passed was a string, not an XML document (of type [xml]).
Typing your $xmlData parameter variable as [xml] solves this problem, as that will implicitly covert even a string argument to an XML document on demand - if possible.
A simplified example, using a script block in lieu of a function:
$xmlString = #'
<?xml version="1.0"?><catalog><book id="bk101"><title>De Profundis</title></book></catalog>
'#
# Note how $xmlData is [xml]-typed.
& { param([xml] $xmlData) $xmlData.catalog.book.title } $xmlString
The above yields De Profundis, demonstrating that the string argument was converted to an [xml] instance (which - thanks to PowerShell's type adaptation magic - makes the element names available as direct properties).
It is then safe to call the .CreateNode() method on $xmlData.
Well, you don't show your original XML format.
Why did you comment out that Get-Content? it will not work without it.
So, if we take the below example, it works as expected.
# Simple XML version
$SimpleXml = $null
$SimpleXml = #"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<name>Apple</name>
<size>1234</size>
</configuration>
"#
# New node code
[xml]$XmlDoc = Get-Content -Path variable:\SimpleXml
$runtime = $XmlDoc.CreateNode("element","runtime","")
$generated = $XmlDoc.CreateNode("element","generatePublisherEvidence","")
$generated.SetAttribute("enabled","false")
$runtime.AppendChild($generated)
$XmlDoc.configuration.AppendChild($runtime)
$XmlDoc.save("$pwd\SimpleXml.xml")
Get-Content -Path "$pwd\SimpleXml.xml"
# Which creates this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<name>Apple</name>
<size>1234</size>
<runtime>
<generatePublisherEvidence enabled="false" />
</runtime>
</configuration>
Also Write-Host is never needed unless you are coloring screen output.
Write-Output is the default and automatically write to the screen, whether you specify Write-Output or not.
So, all of these to the same thing - output to the screen.
$SomeString = 'hello'
Write-Host $SomeString
Write-Output $SomeString
'hello'
"hello"
$SomeString
"$SomeString"
($SomeString)
("$SomeString")
$($SomeString)
# Results
hello
hello
hello
hello
hello
hello
hello
… yet, it's your choice.

Parsing XML and ignoring sections with powershell

I want to parse out the key fields and Data table information from here with PowerShell.
I only want the datatable name if there is a keyfield so in the example below I do not want CC:Attribute.
I also want to output things to a text file.
I want to have a text file that is created that holds the Data table name & Access as well as all the key fields and what they are.
This is the code I have so far:
[xml]$global:xmldata = get-content "C:\hackathon\Mfg.xml"
$xmldata2 = $xmldata.SchemaPackage.Tables
$SField = $xmldata2.DataTable.KeyFields | %{$_.StringField}
$Reffield = $xmldata2.DataTable.KeyFields | %{$_.ReferenceField}
$table = $xmldata2 | %{$_.DataTable}
Xml File:
<?xml version="1.0" encoding="utf-8"?>
<SchemaPackage Namespace="Mfg" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DataTable Name="CC::Attribute">
<DataFields>
</DataFields>
</DataTable>
<DataTable Name="PlannerCode" Access="WW">
<Licenses>Manufacturing, DemandManagement</Licenses>
<Flags>
</Flags>
<KeyFields>
<StringField Name="Value"/>
<ReferenceField Name="Site" Target="Core::Site" SetField="PlannerCodes"/>
</KeyFields>
<DataFields>
<StringField Name="Description"/>
</DataFields>
</DataTable>
</SchemaPackage>
Despite the edit you made, I can only get your XML to validate if I modify it slightly (cleaning the opening XML tag and removing the SchemaPackage namespace). Regardless,
if you're experiencing no issues with your XML import then it's fine.
Here I'm just constructing the XML object from a herestring because I haven't got it in a file on disk.
[xml]$xmldata = #"
<xml>
<DataTable Name="CC::Attribute">
<DataFields>
</DataFields>
</DataTable>
<DataTable Name="PlannerCode">
<Licenses>Manufacturing, DemandManagement</Licenses>
<Flags>
</Flags>
<KeyFields>
<StringField Name="Value"/>
<ReferenceField Name="Site" Target="Core::Site" SetField="PlannerCodes"/>
</KeyFields>
<DataFields>
<StringField Name="Description"/>
</DataFields>
</DataTable>
</xml>
"#
# Filter DataTable nodes for those with a KeyFields child node.
$DataTablesWithKeyFields = $xmldata.xml.DataTable | Where-Object { $_.KeyFields }
$DataTableName = $DataTablesWithKeyFields.Name
$StringFieldData = $DataTablesWithKeyFields.KeyFields.ReferenceField
$ReferenceFieldData = $DataTablesWithKeyFields.KeyFields.ReferenceField
I'm not sure if that's what you're after. $DataTablesWithKeyFields could be an array depending on your XML file so you may need to loop it to extract the information you require.
Since we're working with XML, one of the querying options is XPath!
You can select only DataTable nodes that have a KeyFields child with the following XPath expression:
/SchemaPackage/DataTable[KeyFields]
You can use Select-Xml:
Select-Xml -Path C:\hackathon\Mfg.xml -XPath /SchemaPackage/DataTable[KeyFields] |Select-Object -Expand Node
or pass the expression as an argument to the SelectSingleNodes() method:
[xml]$xmldata = Get-Content C:\hackathon\Mfg.xml
$xmldata.SelectNodes('/SchemaPackage/DataTable[KeyFields]')

Create new element in existing XML Document using Powershell

I have a script that reads from an XML file, performs some web services, from which it gets a response and I'd now like the XML fie to be amended with the webs response as below.
Original XML:
<ItemNum>
<Key>11233</Key>
<Item>123.jpg</Item>
<Item>456.jpg</Item>
<Item>789.jpg</Item>
<Detail>Processed on 8-12-2017</Detail>
</ItemNum>
The script will process the items from the XML and iterate through the itemlist, similar to below:
[xml]$xml = Get-Content -Path $xmlPath
Foreach ($websResponse in $websResponseList) {
<Create New Element>
<Add Webs Response to Element>
}
$xml.save($xmlPath)
The result should be the original XML modified to look like the below:
<ItemNum>
<Key>11233</Key>
<Item>123.jpg</Item>
<Item>456.jpg</Item>
<Item>789.jpg</Item>
<Detail>Processed on 8-12-2017</Detail>
<WebS>00001</WebS>
<WebS>00002</WebS>
<WebS>00003</WebS>
</ItemNum>
I've read countless articles on working with XML in Powershell and probably some that are very similar what I'm trying to achieve but this is for some reason stumping me so would appreciate any pointers.
You're almost there:
[xml]$xml = Get-Content -Path $xmlPath
foreach ($websResponse in $websResponseList)
{
$elem = $xml.CreateElement('WebS','00001') # substitute with data
$xml.ItemNum.AppendChild($elem)
}
$xml.save($xmlPath)

PowerShell: XML to Object

I read out data from a XML-File and want to get an object like this:
TaskSequenceNumber TaskSequenceName
------------------ ----------------
1 01_Base
2 02_ABC
The XML-File Looks like this:
$xml = [xml]#"
<?xml version="1.0" encoding="utf-8"?>
<DeploymentScript>
<Settings>
...
</Settings>
<TaskSequences>
<Sequence1>
<TaskSequenceNumber>1</TaskSequenceNumber>
<TaskSequenceName>01_Base</TaskSequenceName>
</Sequence1>
<Sequence2>
<TaskSequenceNumber>2</TaskSequenceNumber>
<TaskSequenceName>02_ABC</TaskSequenceName>
</Sequence2>
</TaskSequences>
</DeploymentScript>
"#
$xml.DeploymentScript.TaskSequences
The code above is prepared for easy copy & paste into ISE, if anybody wants to try it.
About the name of the nodes Sequence1 and Sequence2 I am not sure yet. Maybe they could be renamed to $TaskSequenceName or $TaskSequenceNumber.
.
EDIT:
I am really sorry, but I need to reopen my request about it.
I was trying to implement the posted solution of Martin, but I came into trouble. Look at the following example:
Clear-Host
Function Simple-Test {
Param
(
[Parameter(Mandatory=$true,position=0)]
[string]$ControlObject
)
$ControlObject | Format-Table
}
$xml = [xml]#"
<?xml version="1.0" encoding="utf-8"?>
<DeploymentScript>
<Settings>
...
</Settings>
<TaskSequences>
<Sequence1>
<TaskSequenceNumber>1</TaskSequenceNumber>
<TaskSequenceName>01_Base</TaskSequenceName>
</Sequence1>
<Sequence2>
<TaskSequenceNumber>2</TaskSequenceNumber>
<TaskSequenceName>02_ABC</TaskSequenceName>
</Sequence2>
</TaskSequences>
</DeploymentScript>
"#
Simple-Test -ControlObject $xml.DeploymentScript.TaskSequences.GetEnumerator()
The script won't work as I would expect. I get the result:
System.Xml.XmlElement System.Xml.XmlElement
How to correct this?
Thank you!
You will get the output by calling the GetEnumerator() method:
$xml.DeploymentScript.TaskSequences.GetEnumerator()
Output:
TaskSequenceNumber TaskSequenceName
------------------ ----------------
1 01_Base
2 02_ABC

xpath soap namespace grabbing items

I am feeling like i am doing something really not correct.
When doing a soap they return me with an xml which may or may not contain an error.
I would like to check if the error exists if not read the values.
somehow, I can't grab it directly :(
Below is a sample return of something with results and one which gives an error (name not found)
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/ccr01/searchPersonResponse</wsaAction>
<wsaMessageID>urn:uuid:b75d2932-5687-4871-9d07-3b74b084978a</wsaMessageID>
<wsaRelatesTo>urn:uuid:9112d870-248d-4d07-acd0-d88e4a48d547</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-061df7b5-32a2-4021-852d-2df98953e076">
<wsuCreated>2011-05-27T12:11:45Z</wsuCreated>
<wsuExpires>2011-05-27T12:16:45Z</wsuExpires>
</wsuTimestamp>
</envHeader>
<soapBody>
<searchPersonResponse xmlns="http://www.rechtspraak.nl/namespaces/ccr01">
<searchPersonResult>
<CCR_WS xmlns="http://www.rechtspraak.nl/namespaces/ccr">
<curandus>
<ccn>1</ccn>
<cur_voornamen>Jan</cur_voornamen>
<cur_voorvoegsels>van</cur_voorvoegsels>
<cur_achternaam>Beek</cur_achternaam>
<geboorte_datum>1980-01-02</geboorte_datum>
<geboorte_plaats>Werkendam</geboorte_plaats>
</curandus>
</CCR_WS>
</searchPersonResult>
</searchPersonResponse>
</soapBody>
</soapEnvelope>
and the one without results
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/ccr01/searchPersonResponse</wsaAction>
<wsaMessageID>urn:uuid:b75d2932-5687-4871-9d07-3b74b084978a</wsaMessageID>
<wsaRelatesTo>urn:uuid:9112d870-248d-4d07-acd0-d88e4a48d547</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-061df7b5-32a2-4021-852d-2df98953e076">
<wsuCreated>2011-05-27T12:11:45Z</wsuCreated>
<wsuExpires>2011-05-27T12:16:45Z</wsuExpires>
</wsuTimestamp>
</envHeader>
<soapBody>
<searchPersonResponse xmlns="http://www.rechtspraak.nl/namespaces/ccr01">
<searchPersonResult>
<CCR_WS xmlns="http://www.rechtspraak.nl/namespaces/ccr">
<exceptie errorcode="1">No Results found.</exceptie>
</CCR_WS>
</searchPersonResult>
</searchPersonResponse>
</soapBody>
</soapEnvelope>
Here is my code to select the namespace, then check
$results = simplexml_load_string($response);
$results->registerXPathNamespace('ccr','http://www.rechtspraak.nl/namespaces/ccr');
$lijst = $results->xpath('//ccr:CCR_WS');
$errorcode = $lijst[0]->exceptie->attributes()->errorcode;
$error = $lijst[0]->exceptie;
if (isset($errorcode) AND $errorcode != "") {
// do things with the error code
} else {
$lijst = $results->xpath('//ccr01:searchPersonResult');
$cur = $lijst[0]->CCR_WS->curandus;
echo $cur->ccn."<BR>";
echo $cur->cur_voornamen."<BR>";
echo $cur->cur_voorvoegsels."<BR>";
echo $cur->cur_achternaam."<BR>";
echo $cur->geboorte_datum."<BR>";
echo $cur->geboorte_plaats."<BR>";
}
surely there is a better way of grabbing
$lijst[0]->exceptie->attributes()->errorcode
for example...
...Don't know if this is a "better way" to everyone, but here is a direct XPath expression to select the errorcode. You can make it shorter and less efficient by dropping steps and using // (in the beginning or in the middle). Attributes are selected with # (or with attribute:: axis if you prefer the longer syntax). If attribute (or the exceptie element) doesn't exist, nothing is returned.
/*/*/ccr01:searchPersonResponse/ccr01:searchPersonResult/ccr:CCR_WS/ccr:exceptie/#errorcode
Remember to register all the namespace prefixes that yo use in your XPath expression.