3

So I have some XML that I'm creating a collection on and then trying to Sort it based on an Element but it's not working.

My Code

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

I would expect the output to be like this

Name                                                        StartOrder
----                                                        ----------
Service1                                                    1
Service2                                                    2
Service3                                                    3
Service4                                                    4

The Output I keep getting is this:

Name                                                        StartOrder
----                                                        ----------
Service1                                                    1
Service2                                                    2
Service4                                                    4
Service3                                                    3

anyone have an idea? I've tried casting the StartOrder to an [int] but that doesn't work either. Any help is appriciated.

1
  • You're sorting each row individually. Have you tried sorting $services.ChildNodes instead? Commented Nov 3, 2015 at 22:43

1 Answer 1

1

This modified version of the function sorted by StartOrder in ascending order for me:

function getServerServiceStatus ( $servers ) 
{
    foreach ( $server in $servers ) 
    {
        $services = ( readServerServicesfromXML $server )
        $services.ChildNodes | Sort-Object { [int] $_.StartOrder}
    }
}

The output is:

Name                                                        StartOrder
----                                                        ----------
Service1                                                    1
Service2                                                    2
Service3                                                    3
Service4                                                    4
Sign up to request clarification or add additional context in comments.

2 Comments

Volkan you rock! Thanks for the assistance. That is exactly what I needed. The online help doc doesn't really help a newbie that much. Can you suggest any online resources for learning the ins and outs of PowerShell.
Thanks for the kind words, Todd. In terms of resources I generally like Pluralsight courses as they cover various aspects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.