我之前在 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 循环中尝试将数组中每个项目的值更改为 Regex 命令的结果时。它似乎创建了一个变量 $c,用正确的值填充它,但永远不要将该值写入 ForEach 正在处理的数组中的当前项。
我也尝试过类似下面的代码(产生错误):
#Regex to replace string in metadata category
$xmli.metadata.category | ForEach-Object
{
$regex = "^[^/]*(.*)$"
$_ -match $regex
$_ = "Subscriptions" + $matches[1]
}
任何想法为什么它不更新每个类别节点的值?