0

I have previously asked this question on the Spiceworks community, but have not received any responses yet. If you guys don't mind i would like to try my luck here. So here goes:

I am working with editing XML files with Powershell. Most of my edits work perfectly fine, except the portion I am updating with whatever comes back from the regex results.

Here is the sample XML section:

<?xml version="1.0" encoding="utf-8"?>
<assetPackages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:xsd="http://www.w3.org/2001/XMLSchema"; verb="">
    <assetPackage asset_name="JackalCLSDMP0802title" type="VOD" providerID="starzencore.com" providerName="MOVIEPLEX" creationDate="2013-07-11" product="MOD" version="1" verb="">
        <metadata>
            <assetID>TITL0020000000864577</assetID>
            <providerID>starzencore.com</providerID>
            <title language="en">The Jackal</title>
            <sortTitle language="en">The Jackal</sortTitle>
        <reducedTitle language="en">The Jackal</reducedTitle>
            <summary language="en">Summary is Long</summary>
            <shortSummary language="en">Shorter Summary</shortSummary>
            <flag>ClosedCaptioning</flag>
            <rating rating_system="MPAA" value="R" />
            <runTimeMinutes>125</runTimeMinutes>
            <release_year>1997</release_year>
            <countryRegionCode>us</countryRegionCode>
            <person role="director" lname="Caton-Jones" fname="Michael" mname="" />
            <person role="actor" lname="Willis" fname="Bruce" mname="" />
            <person role="actor" lname="Gere" fname="Richard" mname="" />
            <studio>UNIVERSAL PAY TELEVISION</studio>
            <category>Premium Channels/Movieplex/All Movies</category>
            <category>Premium Channels/Movieplex/Drama</category>
            <category>Premium Channels/Movieplex/Top Hits</category>
            <targetingProfileId>MoviePlex_VOD</targetingProfileId>
        </metadata>
    </assetPackage>
</assetPackages>

And here is my current snippet of code:

$TempXML = @(Get-ChildItem $tempDir -recurse -filter "Metadata.xml" | Where-Object {$_.FullName -like "*$($like)*"})

    $Results = @()

    ### Edit XML Process
    ForEach ($File in $tempXML)
    {
        $xmlData = [xml](Get-Content "\\Wtcvhovmgmt001\Staging\starzencore.com-TITL0020000000864577\metadata.xml") #(Get-Content $File.Fullname)
        $xmli = $xmlData.assetpackages.assetpackage

        #Regex to replace string in metadata category
        ForEach ($c in ($xmli.metadata.category))
        {
            if ({ $c -notlike "*Subscriptions*" })
            {
                $regex = "^[^/]*(.*)$"
                $c -match $regex
                $c = "Subscriptions" + $matches[1]
                #$c
            }
        }
        $xmlData.Save("\\Wtcvhovmgmt001\Staging\starzencore.com-TITL0020000000864577\metadata.xml") #$xmlData.Save($File.Fullname)

        $DirName = Split-Path (Split-Path $file.Fullname -Parent) -Leaf
        $Results += New-Object PSObject -Property @{
            "Date/Time" = $Date
            File = $($file.FullName)
            "Last Write Time" = $($file.LastWriteTime)
            Directory = $($DirName)
            Status = "Successful"
            }
        Write-Host "Succesfully processed $($File.Fullname)" -foregroundcolor green -backgroundcolor black

        #Write event to event log for successful metadata editing
        #Write-Eventlog -ComputerName "WTCVHOVCTRL001" Application -source "Microsoft IPTV" -eventid 14888 -message "Successfully edited/updated $($File.Fullname). File is ready for reimport"
    }

The problem I am running into is that when I am inside the ForEach loop attempting to change the value of each item in the Array to the results of the Regex command. It appears to create a variable $c, populate it with the correct value, but then never write that value to the current item in the array that the ForEach is processing.

I have also tried something like the below code (that produced errors):

#Regex to replace string in metadata category
$xmli.metadata.category | ForEach-Object
{
    $regex = "^[^/]*(.*)$"
    $_ -match $regex
    $_ = "Subscriptions" + $matches[1]
}

Any ideas why it's not updating the value of each category node?

1 Answer 1

1

Instead of this:

 $xmli = $xmlData.assetpackages.assetpackage

 #Regex to replace string in metadata category
 ForEach ($c in ($xmli.metadata.category))
 {
     if ({ $c -notlike "*Subscriptions*" })
     {
         $regex = "^[^/]*(.*)$"
         $c -match $regex
         $c = "Subscriptions" + $matches[1]
         #$c
     }
 }

try this:

$xmli.SelectNodes('//category[not(starts-with(.,"Subscriptions"))]') | % {
  $_.'#text' = $_.'#text' -replace '^[^/]*(.*)$', 'Subscriptions$1'
}
Sign up to request clarification or add additional context in comments.

2 Comments

This was so close. The solution was correct, but you had a typo, you missed the underscore on the right side of the = Thank you so much. I never thought of using select nodes (still new to editing xml with PowerShell)
Indeed I did. It's fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.