所以我有一些XML,我正在创建一个集合,然后尝试根据一个元素对它进行排序,但它不起作用。
我的代码
$xml = [xml](Get-Content $args)
function readServersfromXML ( ) {
$xml.Settings.Server | select -ExpandProperty Name
}
function readServerServicesfromXML ( $server ) {
$xml.Settings.Server | Where-Object {$_.Name -eq $server} | Select-Object -ExpandProperty Services
}
function getServerServiceStatus ( $servers ) {
foreach ( $server in $servers ) {
$services = ( readServerServicesfromXML $server )
foreach ( $service in $services.ChildNodes ) {
$service | Sort-Object StartOrder
}
}
}
$servers = readServersfromXML
getServerServiceStatus ( $servers )
XML
<?xml version="1.0"?>
<Settings>
<Server>
<Name>Server1</Name>
<StartOrder>1</StartOrder>
<Services>
<Service>
<Name>Service1</Name>
<StartOrder>1</StartOrder>
</Service>
<Service>
<Name>Service2</Name>
<StartOrder>2</StartOrder>
</Service>
<Service>
<Name>Service4</Name>
<StartOrder>4</StartOrder>
</Service>
<Service>
<Name>Service3</Name>
<StartOrder>3</StartOrder>
</Service>
</Services>
</Server>
</Settings>
我希望输出像这样
Name StartOrder
---- ----------
Service1 1
Service2 2
Service3 3
Service4 4
我一直得到的输出是:
Name StartOrder
---- ----------
Service1 1
Service2 2
Service4 4
Service3 3
谁有个主意?我已经尝试将StartOrder转换为[int],但这也不起作用。任何帮助都是适当的。
答案 0 :(得分:1)
这个由StartOrder按升序排序的函数的修改版本对我来说:
function getServerServiceStatus ( $servers )
{
foreach ( $server in $servers )
{
$services = ( readServerServicesfromXML $server )
$services.ChildNodes | Sort-Object { [int] $_.StartOrder}
}
}
输出结果为:
Name StartOrder
---- ----------
Service1 1
Service2 2
Service3 3
Service4 4