将XML转换为PSCustomObject

时间:2019-01-20 19:27:12

标签: xml powershell pscustomobject

我正在尝试将标准XML文档转换为存储在一系列文件夹中的XML文档,将它们聚合在一起以构建自动补丁系统。 XML文档格式提供了灵活性/易用性的最佳组合。不幸的是,一旦将XML文档格式化,PowerShell的XML子系统就区分大小写,这可能会留出不必要的麻烦,因此我试图将导入的XML文档转换为PSCustomObjects,但我遇到了麻烦。

我无法找到一种方法来检测特定属性是否存在子节点,因此我可以再次通过Convert-XMLtoArray进行重申,以便它将所有XML子节点转换为PSCustomObjects。 / p>

结果:

Application     Version InstallType Installers
-----------     ------- ----------- ----------
Mozilla Firefox 64.0.2  Install               

预期结果:

Application     Version InstallType Installers
-----------     ------- ----------- ----------
Mozilla Firefox 64.0.2  Install     {Windows 10,Windows7...}     

代码:

function Convert-XmltoArray($xml) {
    $Return = New-Object -TypeName 'PSCustomObject'
    $XML | Get-Member -MemberType Property | ForEach {
        $Property = New-Object -TypeName 'PSCustomObject'
        $Name = $_.name
        $Value = $XML.($Name)
        if ($Value.HasChildNodes) {
            foreach ($Child in $Value.ChildNodes) {
                $Return | Add-Member -Type NoteProperty -Name $Child.localname -Value $($Child.'#text')
                #<SomethingHere>
            }
        }
    }
    $Return
}

$Test = [XML]@"
<Package>
    <Application>Java</Application>
    <Version>8.2.9.23</Version>
    <InstallType>Install</InstallType>
    <Installers>
        <Windows10>
            <x86>
                <File1>
                    <FileName>jre-8u201-windows-i586.exe</FileName>
                    <Parameters>/s</Parameters>
                </File1>
            </x86>
            <x64>
                <file1>
                    <FileName>jre-8u201-windows-x64.exe</FileName>
                    <Parameters>/s</Parameters>
                </file1>
            </x64>
            <IA64>
                <File1>
                    <FileName></FileName>
                    <Parameters></Parameters>
                    <CustomSuccessCodes></CustomSuccessCodes>
                    <CustomErrorCodes></CustomErrorCodes>
                </File1>
            </IA64>
        </Windows10>
        <Windows7>
            <x86>
                <File1>
                    <FileName>jre-8u201-windows-i586.exe</FileName>
                    <Parameters>/s</Parameters>
                </File1>
            </x86>
            <x64>
                <file1>
                    <FileName>jre-8u201-windows-x64.exe</FileName>
                    <Parameters>/s</Parameters>
                </file1>
            </x64>
            <IA64>
                <File1>
                    <FileName></FileName>
                    <Parameters></Parameters>
                    <CustomSuccessCodes></CustomSuccessCodes>
                    <CustomErrorCodes></CustomErrorCodes>
                </File1>
            </IA64>
        </Windows7>
    </Installers>
</Package>
"@
$Result = Convert-XMLToArray -xml $test
$Result

更新

最终弄清楚了它,甚至与PSv2兼容。

Function Convert-XMLtoPSObject {
    Param (
        $XML
    )
    $Return = New-Object -TypeName PSCustomObject
    $xml |Get-Member -MemberType Property |Where-Object {$_.MemberType -EQ "Property"} |ForEach {
        IF ($_.Definition -Match "^\bstring\b.*$") {
            $Return | Add-Member -MemberType NoteProperty -Name $($_.Name) -Value $($XML.($_.Name))
        } ElseIf ($_.Definition -Match "^\System.Xml.XmlElement\b.*$") {
            $Return | Add-Member -MemberType NoteProperty -Name $($_.Name) -Value $(Convert-XMLtoPSObject -XML $($XML.($_.Name)))
        } Else {
            Write-Host " Unrecognized Type: $($_.Name)='$($_.Definition)'"
        }
    }
    $Return
}

1 个答案:

答案 0 :(得分:2)

你永远不会得到这个……

{Windows 10,Windows7...}  

…根据发布的XML示例,它不包含多个OS版本。

这里是一个示例方法,将填充“安装程序”列,但仅基于您发布的XML示例。

$Test = [XML]@"
<package>
    <Application>Mozilla Firefox</Application>
    <Version>64.0.2</Version>
    <InstallType>Install</InstallType>
    <Installers>
        <Windows10>
            <x86>
                <File1>
                    <FileName>Firefox_Setup_64.0.2_x86.exe</FileName>
                    <Parameters>/s</Parameters>
                </File1>
            </x86>
            <x64>
                <file1>
                    <FileName>Firefox_Setup_64.0.2_x64.exe</FileName>
                    <Parameters>/s</Parameters>
                </file1>
            </x64>
            <IA64>
                <File1>
                    <FileName></FileName>
                    <Parameters></Parameters>
                    <CustomSuccessCodes></CustomSuccessCodes>
                    <CustomErrorCodes></CustomErrorCodes>
                </File1>
            </IA64>
        </Windows10>
    </Installers>
</package>
"@

function ConvertFrom-XmlPart($xml)
{
    $hash = @{}

    $xml | 
    Get-Member -MemberType Property |
        % {
        $name = $_.Name
        if ($_.Definition.StartsWith("string "))
        {
            $hash.($Name) = $xml.$($Name)
        }
        elseif ($_.Definition.StartsWith("System.Object[] "))
        {
            $obj = $xml.$($Name)
            $hash.($Name) = $($obj | 
            % { $_.tag }) -join "; "
        }
        elseif ($_.Definition.StartsWith("System.Xml"))
        {
            $obj = $xml.$($Name)
            $hash.($Name) = @{}
            if ($obj.HasAttributes)
            {
                $attrName = $obj.Attributes | 
                Select-Object -First 1 | 
                % { $_.Name }

                if ($attrName -eq "tag")
                {
                    $hash.($Name) = $($obj | 
                    % { $_.tag }) -join "; "
                }
                else
                {
                    $hash.($Name) = ConvertFrom-XmlPart $obj
                }
            }
            if ($obj.HasChildNodes)
            {
                $obj.ChildNodes | 
                % { $hash.($Name).($_.Name) = ConvertFrom-XmlPart $($obj.$($_.Name)) }
            }
        }
    }
    return $hash
}

function ConvertFrom-Xml($xml) 
{
    $hash = @{}
    $hash = ConvertFrom-XmlPart($xml)
    return New-Object PSObject -Property $hash
}

ConvertFrom-XmlPart -xml $Test

# Output:
# =======
# Name                           Value
# ----                           -----
# package                        {InstallType, Version, Installers, Application}

# Walking the data
$Test.package

# Output:
# =======
# Application     Version InstallType Installers
# -----------     ------- ----------- ----------
# Mozilla Firefox 64.0.2  Install     Installers


$Test.package.Installers

# Output:
# =======
# Windows10
# ---------
# Windows10