CUCMSidecarWorkflow/Private/ConvertTo-SortedSidecar.ps1

function ConvertTo-SortedSidecar
{

    <#
    .SYNOPSIS
     Sorts the directories on a device's sidecar.
 
     .DESCRIPTION
     Sort-BusyLampFields grabs the necessary information of each directory on the device's sidecar given in the DeviceInfo.xml file generated from Get-DeviceInfo.Following, it creates a new XML document (sorted.xml) which sorts the given directories alphabetically.
 
     .PARAMETER FileConent
     The XML content contained in the FileName (DeviceInfo.xml) parameter of Get-DeviceInfo.
 
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true)]
        [xml]$FileContent
    )

    BEGIN{}

    PROCESS
    {
        #Create New XML Doc to hold sorted lamp fileds.
        $SortedFile = "C:\users\$env:USERNAME\Desktop\Sorted.xml"
        $DefaultRoutePartition = "Internal_DN-PT"
        $NewXMLDoc = New-Object System.Xml.XmlDocument
        [System.XML.XMLElement]$Root = $NewXMLDoc.CreateElement("busyLampFields")
        [void]$NewXMLDoc.AppendChild($Root)

        #Sort the nodes currently on the sidecar.
        $BLFs = $FileContent.SelectNodes("//busyLampFields/busyLampField")
        $SortedBLFs = $BlFs | Sort-Object -Property label
        $index = 0 
        
        #Take sorted nodes, and add each as an XML element in the new XML Document.
        foreach($field in $SortedBLFs)
        {
            #Create root node of the XML document, 'busyLampField'
            $NewBLFElement = $NewXMLDoc.CreateElement("busyLampField")
            [void]$Root.AppendChild($NewBLFElement)

            #Create blfdirn element, and and directory number.
            $NewDirn = $NewXMLDoc.CreateElement("blfDirn")
            $DirnText = $NewXMLDoc.CreateTextNode($field.blfDirn)
            [void]$NewDirn.AppendChild($DirnText) 
            [void]$NewBLFElement.AppendChild($NewDirn) 

            #Create routePartition element, and add the value.
            $NewRoutePartition = $NewXMLDoc.CreateElement("routePartition")
            $NewPartitionText = $NewXMLDoc.CreateTextNode($DefaultRoutePartition)
            [void]$NewRoutePartition.AppendChild($NewPartitionText)
            [void]$NewBLFElement.AppendChild($NewRoutePartition)

            #Create new label element, and add the corresponding text.
            $NewLabel = $NewXMLDoc.CreateElement("label")
            $NewLabelText = $NewXMLDoc.CreateTextNode($field.label)
            [void]$NewLabel.AppendChild($NewLabelText)
            [void]$NewBLFElement.AppendChild($NewLabel)
            
            #Create new index element, and increment by 1 for each busy lamp field.
            $index = $index + 1
            $IndexNode = $NewXMLDoc.CreateElement("index")
            $IndexText = $NewXMLDoc.CreateTextNode($index)
            [void]$IndexNode.AppendChild($IndexText)
            [void]$NewBLFElement.AppendChild($IndexNode)
        }
        [void]$NewXMLDoc.save($SortedFile)


    } #PROCESS
    END{}
}