Forum Discussion
SteveCox
Feb 08, 2022Brass Contributor
new content after last match of a Pattern in XML file
Hi I am trying to use Powershell to add content to an XML file after the last match of a pattern I need to add the content from $OutTemp txt file "<!-- steve.cox - test3 -02/02/2022 11:01:36 --> ...
- Feb 12, 2022Glad it help.
Kindly mark the answer as best response.
For the XML, I need to check it, and also you can start a new post as it also be better in the search engines.
Thanks
farismalaeb
Feb 09, 2022Steel Contributor
Hi
I think this should help
$NewLines = Get-Content -Path C:\Users\f.malaeb\OutTemp.txt | ?{$_.Trim() -ne ""}
[System.Collections.ArrayList]$New = Get-Content -Path C:\Users\f.malaeb\XML.xml
$IndexNumber=(($new | Select-String "<InstancePathId>").LineNumber)[-1]
$new.Insert($IndexNumber,$NewLines)
$new
The Expected output is
<Groups>
<Group Id="{9b28fae8-72f7-4267-a1a5-685f747a7146}">
<!-- ./Vendor/MSFT/Defender/Configuration/DeviceControl/PolicyGroups/%7b9b28fae8-72f7-4267-a1a5-685f747a7146%7d/GroupData -->
<MatchType>MatchAny</MatchType>
<DescriptorIdList>
<PrimaryId>RemovableMediaDevices</PrimaryId>
<PrimaryId>CdRomDevices</PrimaryId>
<PrimaryId>WpdDevices</PrimaryId>
</DescriptorIdList>
</Group>
<Group Id="{65fa649a-a111-4912-9294-fb6337a25038}">
<MatchType>MatchAny</MatchType>
<DescriptorIdList>
<InstancePathId>USBSTOR\DISK&VEN_TOSHIBA&PROD_SILICON-POWER&REV_PMAP\MSFTNAKTM10708466023196668&0</InstancePathId>
<InstancePathId>USBSTOR\DISK&VEN_KINGSTON&PROD_DATATRAVELER_2.0&REV_0000\408D5C1EF726102179650693&0</InstancePathId>
<InstancePathId>USBSTOR\DISK&VEN_KINGSTON&PROD_DATATRAVELER_3.0&REV_PMAP\60A44C42651BB2A04626FC36&0</InstancePathId>
<InstancePathId>USBSTOR\DISK&VEN_SANDISK&PROD_CRUZER_EDGE&REV_1.26\20043514001B91029E82&0</InstancePathId>
<!-- steve.cox - test3 -02/02/2022 11:01:36 -->
<InstancePathId>USBSTOR\DISK&VEN_KINGSTON&PROD_DATATRAVELER_2.0&REV_PMAP\5B8207000061&0</InstancePathId>
<!-- steve.cox - test4 - -02/02/2022 11:01:36 -->
<InstancePathId>USBSTOR\DISK&VEN_TOSHIBA&PROD_SILICON-POWER&REV_PMAP\MSFTNAKTM1070837BA061C1622&0</InstancePathId>
</DescriptorIdList>
</Group>
</Groups>