CUCMSidecarWorkflow/Public/Edit-Sidecar.ps1

function Edit-Sidecar
{ 
<#
     .SYNOPSIS
     Calls the functions necessary for editing a sidecar for a Cisco device.
 
     .DESCRIPTION
     Edit-SideCar initally asks the user for credentials to access the CUCM database. It then calls Get-DeviceInfo to gather initial infomation about the device to be updated. Following, the user is presented with the option of adding or removing a user from the sidecar. The user may also chose to quit. The function runs until the user enters 'Q'. If 'Q' is chosen, the necessary files that were created on the user's desktop while running the module are removed.
 
 
     .PARAMETER DeviceName
     The Cisco device (phone) to be updated.
 
     .PARAMETER FileName
     The XML document which holds the infomation of the device to be updated. Created from the SOAP response given from the AXL API. Default is "C:\Users\$env:USERNAME\Desktop\phoneInfo.xml"
      
     .EXAMPLE
     Edit-SideCar -DeviceName SEP000236598
   #>


    [CmdletBinding()]
    Param
    (

        [Parameter(Mandatory=$true, 
                   HelpMessage='Device Name of the Cisco phone to edit.')]
        [string]$DeviceName,


        [string]$FileName ="C:\Users\$env:USERNAME\Desktop\DeviceInfo.xml"

        
    )

    Begin{}

    Process
    {
         #$CUCMServer = "CUCM_SERVER" #TODO: Add your CUCM instance.
       #Credential request for access to CUCM.
       if(!$cred)
       {
         $cred = Get-Credential -Message "Credentials are required to access CUCM. Please enter them now."
       }

      
       Get-DeviceInfo $DeviceName $FileName $cred
       $FileContent = [xml](Get-content $FileName)
       Write-Output "These are the current directories listed on the sidecar: "
        $BLFs = $FileContent.SelectNodes("//busyLampFields/busyLampField")
        $SortedBLFs = $BlFs | Sort-Object -Property label
        foreach($dir in $SortedBLFs)
        {
            $dir.index + ":`t" + $dir.label
        }
    
        $choice = $true
        while($choice)
        {
            
            $choice = Read-Host "Do you want to adQd or remove a user? (A)dd, (R)emove, or (Q)uit"

            if($choice -eq 'A')
            {
                #Grab updated info after add/remove
                Write-Verbose "Sending SOAP request to gather updated information about $DeviceName" -verbose
                Get-DeviceInfo $DeviceName $FileName $cred
                Add-UserToSidecar $FileName
                
            }

            if($choice -eq 'R')
            {   
                #Grab updated info after add/remove
                Write-Verbose "Sending SOAP request to gather updated information about $DeviceName" -verbose
                Get-DeviceInfo $DeviceName $FileName $cred
                Remove-UserFromSidecar $FileName
                
            }

            if($choice -eq 'Q')
            {
                Write-Verbose 'Exiting...!' -Verbose

                #Removing the files created by the program from the user's desktop.
                $FileName = "C:\users\$env:USERNAME\desktop\DeviceInfo.xml"
                if(Test-Path $FileName)
                {
                    Remove-Item $FileName
                }

                $SortedFile = "C:\users\$env:USERNAME\desktop\Sorted.xml"
                if(Test-Path $SortedFile)
                {
                   Remove-Item $SortedFile
                }

                return #exit program
             }
        }
    }
    End {}
}