在PowerShell中逐个获取对象属性

时间:2014-03-28 15:28:57

标签: object powershell

我有一个读取XML文件的程序。在这些XML文件中,列出了一些未启用的语言代码。我创建了一个具有文件路径和每个语言环境属性的对象。我希望我的脚本遍历每个XML文件,如果没有列出区域设置将X放在正确的区域设置下。这就是我到目前为止所做的:

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"

我一直在尝试获取Objects属性名称,然后循环遍历每个名​​称,如果它匹配XML的disableLocales部分中列出的语言环境,则插入一个X.但是我真的很难做到这一点。< / p>

这是我正在查看的XML文件的一部分:

<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>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我对你想要&#39; X&#39;的位置感到模糊,但也许这有帮助吗?

$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