Reputation: 1423
I have a program that reads XML files. In these XML files there are entries listing some language codes that aren't enabled. I have created an object with properties for the file path and each locale. I want my script to go through each XML file, and if a locale is not listed to put an X under the correct locale. This is what I have so far:
Get-ChildItem C:\Users\rasuser\Desktop\ *.xml -recurse |
% {
$contentItem = [xml](Get-Content $_.fullname)
$contentItem = $contentItem.ExportedContentItem
#$contentItem
$resultRow = New-Object -Type PSObject -Property @{
'FilePath' = Join-Path $contentItem.path $contentItem.name
'InvariantCulture' = ''
'ar-ae' = ''
'ar-sa' = ''
'cs-cz' = ''
'da-dk' = ''
'de-at' = ''
'de-ch' = ''
'de-DE' = ''
'el-gr' = ''
'en-au' = ''
'en-ca' = ''
'en-gb' = ''
'en-hk' = ''
'en-ie' = ''
'en-in' = ''
'en-nz' = ''
'en-sg' = ''
'en-us' = ''
'en-za' = ''
'es-ar' = ''
'es-cl' = ''
'es-co' = ''
'es-es' = ''
'es-mx' = ''
'fi-fi' = ''
'fr-be' = ''
'fr-ca' = ''
'fr-ch' = ''
'fr-fr' = ''
'he-il' = ''
'hu-hu' = ''
'it-it' = ''
'ja-jp' = ''
'ko-kr' = ''
'nb-no' = ''
'nl-be' = ''
'nl-nl' = ''
'pl-pl' = ''
'pt-br' = ''
'pt-pt' = ''
'ru-ru' = ''
'sk-sk' = ''
'sv-se' = ''
'tr-tr' = ''
'zh-hk' = ''
'zh-tw' = ''
}
Join-Path $contentItem.path $contentItem.name
$contentItem.ContentItem.PresentationData.DisabledLocales.locale | % {
#$_
#write-host "ha"
if ($_){
#Write-Host $resultRow."$($_)"
}
}
#$resultRow
} #| Export-Csv '.\text.txt' -NoType -Delimiter "`t"
I have been trying to get the Objects property names, then loop through each name and if it matches a locale listed in the disableLocales section of the XML then insert an X. However I'm really struggling to get that far.
This is the part of the XML file I'm looking at:
<PresentationData>
<DisabledLocales>
<Locale>ar-AE</Locale>
<Locale>ar-SA</Locale>
<Locale>cs-CZ</Locale>
<Locale>da-DK</Locale>
<Locale>de-AT</Locale>
<Locale>de-CH</Locale>
<Locale>el-GR</Locale>
<Locale>en-AE</Locale>
<Locale>en-AU</Locale>
<Locale>en-CA</Locale>
<Locale>en-GB</Locale>
<Locale>en-HK</Locale>
<Locale>en-IE</Locale>
<Locale>en-IN</Locale>
<Locale>en-NZ</Locale>
<Locale>en-SG</Locale>
<Locale>en-ZA</Locale>
<Locale>es-AR</Locale>
<Locale>es-CL</Locale>
<Locale>es-CO</Locale>
<Locale>es-ES</Locale>
<Locale>es-MX</Locale>
<Locale>fi-FI</Locale>
<Locale>fr-BE</Locale>
<Locale>fr-CA</Locale>
<Locale>fr-CH</Locale>
<Locale>fr-FR</Locale>
<Locale>he-IL</Locale>
<Locale>hu-HU</Locale>
<Locale>it-IT</Locale>
<Locale>ja-JP</Locale>
<Locale>ko-KR</Locale>
<Locale>nb-NO</Locale>
<Locale>nl-BE</Locale>
<Locale>nl-NL</Locale>
<Locale>pl-PL</Locale>
<Locale>pt-BR</Locale>
<Locale>pt-PT</Locale>
<Locale>ru-RU</Locale>
<Locale>sk-SK</Locale>
<Locale>sv-SE</Locale>
<Locale>tr-TR</Locale>
<Locale>zh-HK</Locale>
<Locale>zh-TW</Locale>
<Locale>en-US</Locale>
</DisabledLocales>
</PresentationData>
Any help would be greatly appreciated.
Upvotes: 0
Views: 150
Reputation: 1983
I'm fuzzy on where you want the 'X', but maybe this helps?
$hereStr = @"
'ar-ae' = ''
'ar-sa' = ''
'cs-cz' = ''
'da-dk' = ''
'de-at' = ''
'de-ch' = ''
'de-DE' = ''
'el-gr' = ''
'en-au' = ''
'en-ca' = ''
'en-gb' = ''
'en-hk' = ''
'en-ie' = ''
'en-in' = ''
'en-nz' = ''
'en-sg' = ''
'en-us' = ''
'en-za' = ''
'es-ar' = ''
'es-cl' = ''
'es-co' = ''
'es-es' = ''
'es-mx' = ''
'fi-fi' = ''
'fr-be' = ''
'fr-ca' = ''
'fr-ch' = ''
'fr-fr' = ''
'he-il' = ''
'hu-hu' = ''
'it-it' = ''
'ja-jp' = ''
'ko-kr' = ''
'nb-no' = ''
'nl-be' = ''
'nl-nl' = ''
'pl-pl' = ''
'pt-br' = ''
'pt-pt' = ''
'ru-ru' = ''
'sk-sk' = ''
'sv-se' = ''
'tr-tr' = ''
'zh-hk' = ''
'zh-tw' = ''
"@
$array = $hereStr.Split("`n").Trim()
$file = gci file.xml
$contentItem = [xml](Get-Content $file.fullname)
$disabledLocales = $contentItem.PresentationData.DisabledLocales.Locale
$outs = @()
foreach ($item in $array) {
$lang = $item[1..5] -join ""
if ($disabledLocales -contains $lang) {$out = $item -replace "''","'X'"}
else {$out = $item}
$outs += $out
} #close foreach item
$outs
Upvotes: 1