使用powershell对自定义psobject进行排序

qlckcl4x  于 2023-04-30  发布在  Shell
关注(0)|答案(1)|浏览(116)

我在powershell中有一个自定义对象的集合,具有以下属性:
| PackageName|PackageVersion|文件名|
| --------------|--------------|--------------|
| iCoreTestPackage| www.example.com |D:\test.csproj|
| iCoreTestPackage| www.example.com |D:\test2.csproj|
| iCoreTestPackage|最新动态|D:\test2.csproj|
| 巢|6.1.0|D:\GlobalSearch。CSProj|
| System.Json |4.0.20126.16343|D:\GlobalSearch。CSProj|
| Ionic.Zip | www.example.com |D:\test3.csproj|
| Ionic.Zip | www.example.com |D:\test4.csproj|
| Ionic.Zip |最新动态|D:\test4.csproj|
我如何创建一个新的xml,它看起来像:

<?xml version="1.0" encoding="utf-8"?>
<!--Nuget packages and their versions.-->
<NugetPackages>
  <iCoreTestPackage>
    <NuGetVersion>1.0.0.15</NuGetVersion>
    <File>D:\test.csproj</File>
    <File>D:\test2.csproj</File>
    <NuGetVersion>LATEST</NuGetVersion>
    <File>D:\test2.csproj</File>
  </iCoreTestPackage>
  <NEST>
    <NuGetVersion>6.1.0</NuGetVersion>
    <File>D:\GlobalSearch.csproj</File>
  </NEST>
  <System.Json>
    <NuGetVersion>4.0.20126.16343</NuGetVersion>
    <File>D:\GlobalSearch.csproj</File>
  </System.Json>
  <Ionic.Zip>
    <NuGetVersion>1.9.1.8</NuGetVersion>
    <File>D:\test3.csproj</File>
    <File>D:\test4.csproj</File>
    <NuGetVersion>LATEST</NuGetVersion>
    <File>D:\test4.csproj</File>
  </Ionic.Zip>

非常感谢!谢谢!

relj7zay

relj7zay1#

我会用XmlTextWriter来做这个。
使用示例数据:

$data = @'
"PackageName","PackageVersion","FileName"
"iCoreTestPackage","1.0.0.15","D:\test.csproj"
"iCoreTestPackage","1.0.0.15","D:\test2.csproj"
"iCoreTestPackage","LATEST","D:\test2.csproj"
"NEST","6.1.0","D:\GlobalSearch.csproj"
"System.Json","4.0.20126.16343","D:\GlobalSearch.csproj"
"Ionic.Zip","1.9.1.8","D:\test3.csproj"
"Ionic.Zip","1.9.1.8","D:\test4.csproj"
"Ionic.Zip","LATEST","D:\test4.csproj"
'@ | ConvertFrom-Csv

$fileOut = 'D:\Test\NugetPackages.xml'

# create the XmlTextWriter object
$writer = [System.XMl.XmlTextWriter]::new($fileOut, [System.Text.Encoding]::UTF8)
$writer.Formatting  = 'Indented'
$writer.Indentation = 2
$writer.IndentChar  = ' '
# create the XML declaration
$writer.WriteStartDocument()
$writer.WriteComment('Nuget packages and their versions.')
$writer.WriteStartElement('NugetPackages')
# loop through the data grouped on the PackageName property
$data | Group-Object PackageName | ForEach-Object {
    $writer.WriteStartElement($_.Name)  # start node for PackageName
    # group again on the PackageVersion property and loop through that
    $_.Group | Group-Object PackageVersion | ForEach-Object {
        $writer.WriteElementString('NuGetVersion', $_.Name)
        foreach ($item in $_.Group) {
            $writer.WriteElementString('File', $item.FileName)
        }
    }
    $writer.WriteEndElement()  # close node for PackageName
}

$writer.WriteEndElement()  # close node NugetPackages
$writer.WriteEndDocument() # close the document
# clean-up
$writer.Flush()
$writer.Dispose()

结果:

<?xml version="1.0" encoding="utf-8"?>
<!--Nuget packages and their versions.-->
<NugetPackages>
  <iCoreTestPackage>
    <NuGetVersion>1.0.0.15</NuGetVersion>
    <File>D:\test.csproj</File>
    <File>D:\test2.csproj</File>
    <NuGetVersion>LATEST</NuGetVersion>
    <File>D:\test2.csproj</File>
  </iCoreTestPackage>
  <NEST>
    <NuGetVersion>6.1.0</NuGetVersion>
    <File>D:\GlobalSearch.csproj</File>
  </NEST>
  <System.Json>
    <NuGetVersion>4.0.20126.16343</NuGetVersion>
    <File>D:\GlobalSearch.csproj</File>
  </System.Json>
  <Ionic.Zip>
    <NuGetVersion>1.9.1.8</NuGetVersion>
    <File>D:\test3.csproj</File>
    <File>D:\test4.csproj</File>
    <NuGetVersion>LATEST</NuGetVersion>
    <File>D:\test4.csproj</File>
  </Ionic.Zip>
</NugetPackages>

相关问题