Get-TwilioRecording.ps1

function Get-TwilioRecording
{
    <#
    .Synopsis
        Gets Twilio call recordings
    .Description
        Get recordings from Twilio calls
    .Example
        Get-TwilioRecording
    .Link
        Twilio.com
    .Link
        Get-PhoneCall
    #>


    param(    
    # The credential used to get the texts
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [Management.Automation.PSCredential]
    $Credential,
    
    
    # A setting storing the credential
    [Parameter(ValueFromPipelineByPropertyName=$true)]   
    [string[]]
    $Setting = @("TwilioAccountKey", "TwilioAccountSecret"),
    
    # A call SID
    [Parameter(ValueFromPipelineByPropertyName=$true)]   
    [string]
    $CallSid,
    
    [Parameter(ValueFromPipelineByPropertyName=$true)]   
    [Switch]
    $AsMp3,
    
    [Parameter(ValueFromPipelineByPropertyName=$true)]   
    [Switch]
    $AsWav
    
    
    )
    
    process {
        #region Fetch Credential
        if (-not $Credential -and $Setting) {
            if ($setting.Count -eq 1) {

                $userName = Get-WebConfigurationSetting -Setting "${Setting}_UserName"
                $password = Get-WebConfigurationSetting -Setting "${Setting}_Password"
            } elseif ($setting.Count -eq 2)  {
                $userName = Get-secureSetting -Name $Setting[0] -ValueOnly
                $password= Get-secureSetting -Name $Setting[1] -ValueOnly
            }

            if ($userName -and $password) {                
                $password = ConvertTo-SecureString -AsPlainText -Force $password
                $credential  = New-Object Management.Automation.PSCredential $username, $password 
            } elseif ((Get-SecureSetting -Name "$Setting" -ValueOnly | Select-Object -First 1)) {
                $credential = (Get-SecureSetting -Name "$Setting" -ValueOnly | Select-Object -First 1)
            }                        
        }

        if (-not $Credential) {
            Write-Error "No Credential Provided"
            return

        }
        #endregion
        
        #region Get Recordings
        if (-not $Credential) {
            Write-Error "No Twilio Credential provided. Use -Credential or Add-SecureSetting TwilioAccountDefault -Credential (Get-Credential) first"               
            return
        }

        $getWebParams = @{
            WebCredential=$Credential
            Url="https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/Recordings?"           
            AsXml =$true            
            UseWebRequest = $true
        }   
        
        if ($psBoundParameters.CallSid) {
            $getwebParams.Url += "&CallSid=$callsid"
        }     
        
        do {        
            $twiResponse = try {
            Get-Web @getwebParams  |            
                Select-Object -ExpandProperty TwilioResponse 
            } catch {
                $null
                Write-Error $_
                return
            }
            
            if ($twiResponse.Recordings) {
            
                $twiResponse.Recordings | 
                    Select-Object -ExpandProperty Recording |
                    ForEach-Object {
                        if ($AsMp3) {
                            Get-Web -Url ("https://api.twilio.com" + $_.Uri + ".mp3") -asbyte
                        } elseif ($AsWav) {
                            Get-Web -Url ("https://api.twilio.com" + $_.Uri + ".wav") -asbyte
                        } else {
                            $_
                        }
                    }
            
                if ($twiResponse.Recordings.NextPageUri) {
                    $getWebParams.Url="https://api.twilio.com" + $twiResponse.Recordings.NextPageUri
    
                }

            }
        } while ($twiResponse.Recordings.NextPageUri)
        #endregion Get Recordings
              
    }       

}