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. This is mandatory.
 
     .PARAMETER CUCMServer
     The address of the CUCM Instance. This is mandatory.
 
     .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 -CUCMServer nameofserver.domain.com
   #>


    [CmdletBinding()]
    Param
    (

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

        [Parameter(Mandatory=$true, HelpMessage='Address of the CUCM Instance')]
        [string]$CUCMServer,


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

        
    )

    Begin {}

    Process
    {
       #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 $CUCMServer
       
        $choice = $true
        while($choice)
        {
            $choice = Read-Host "Do you want to add or remove a user? (A)dd, (R)emove, (L)ist, 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 $CUCMServer

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

                Remove-UserFromSidecar $FileName
                
            }
            if($choice -eq 'L')
            {
               
                Write-Verbose "Gathering sidecar informaAtion." -Verbose
                Get-DeviceInfo $DeviceName $FileName $cred $CUCMServer
                ConvertTo-SortedSidecar $FileName
                $SortedFile = "C:\users\$env:USERNAME\desktop\Sorted.xml"
                Show-SortedSidecar $SortedFile
            }
            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 {}
}