Feb 08 2022 04:26 AM
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 -->
<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>"
To below the last </InstancePathId> line of the USB-Groups.XML File
<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>
</DescriptorIdList>
</Group>
</Groups>"
If I use
"
Function WritetoXML {
$NewLines = Get-Content -Path $OutTemp | ?{$_.Trim() -ne ""}
$New = Get-Content -Path $XMLPath |
ForEach-Object {
$_
If($_-match ('</InstancePathId>'))
{
$NewLines
}
}
$New | Out-File -FilePath $XMLPath -Force
Remove-Item $OutTemp
}"
it writes after each instance of '</InstancePathId>' but cannot work out how to get this to write to only after the last instance
Feb 08 2022 10:58 PM - edited Feb 08 2022 10:59 PM
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>
Feb 10 2022 01:38 AM
Feb 12 2022 01:12 PM
Solution