我之前在Spiceworks社区上提出了这个问题,但尚未收到任何回复。如果你们不介意我想在这里试试运气。所以这里:
我正在使用Powershell编辑XML文件。我的大多数编辑工作都非常好,除了我正在使用正则表达式结果返回的任何部分进行更新。
以下是XML示例部分:
<?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>
这是我目前的代码片段:
$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"
}
我遇到的问题是当我在ForEach循环内部尝试将Array中每个项的值更改为Regex命令的结果时。它似乎创建了一个变量$ c,用正确的值填充它,但是从不将该值写入ForEach正在处理的数组中的当前项。
我也尝试过类似下面的代码(产生错误):
#Regex to replace string in metadata category
$xmli.metadata.category | ForEach-Object
{
$regex = "^[^/]*(.*)$"
$_ -match $regex
$_ = "Subscriptions" + $matches[1]
}
为什么它没有更新每个类别节点的价值?
答案 0 :(得分:1)
而不是:
$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
}
}
试试这个:
$xmli.SelectNodes('//category[not(starts-with(.,"Subscriptions"))]') | % {
$_.'#text' = $_.'#text' -replace '^[^/]*(.*)$', 'Subscriptions$1'
}