Confirm-IncomingNumber.ps1

function Confirm-IncomingNumber
{
    <#
    .Synopsis
        Confirms a phone number to accept incoming calls
    .Description
         
    .Example
    #>
    
    param(
    # The phone number that is being confirmed. The number _must_ be in [E.164](http://en.wikipedia.org/wiki/E.164) (+${CountryCode}{Number})
    [Parameter(Mandatory=$true,ParameterSetName='SpecificNumber',ValueFromPipelineByPropertyName=$true)]
    [string]
    $PhoneNumber,

    # The area code
    [Parameter(Mandatory=$true,ParameterSetName='RandomNumberInAreaCode',ValueFromPipelineByPropertyName=$true)]
    [string]
    $AreaCode,

    # A friendly name for the phone number
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [string]
    $FriendlyName,

    # The voice URL
    [string]
    $VoiceUrl,

    # The voice URL
    [ValidateSet("GET", "POST")]
    [string]
    $VoiceMethod,

    # The SMS Url
    [string]
    $SmsUrl,

    # The SMS Method
    [ValidateSet("GET", "POST")]
    [string]
    $SmsMethod,


    # The extension to dial
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [string]
    $Extension,

    # A status callback that will receive a message when the person confirms the number
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [string]
    $StatusCallback,
    
    # The method used for the statuc callback
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [string]
    $StatusCallbackMethod,

    # The Twilio credential.
    [Management.Automation.PSCredential]
    $Credential,

    # A setting storing the credential
    [Parameter(ValueFromPipelineByPropertyName=$true)]   
    [string[]]
    $Setting = @("TwilioAccountKey", "TwilioAccountSecret")
    
    )

    process {
    
        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

        }
        $webParams = @{}

        foreach ($key in 'PhoneNumber', 'FriendlyName', 'AreaCode', 'VoiceUrl', 'VoiceMethod', 'SmsUrl', 'SmsMethod') {
            if ($PSBoundParameters[$key]) {
                $webParams[$key] = $PSBoundParameters[$key]
            }            
        }


        $getWebParams = @{
            WebCredential=$Credential
            Url="https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/IncomingPhoneNumbers/"           
            Parameter = $webParams
            Method = "POST"
            UseWebRequest = $true
        }        
        
        Get-Web @getwebParams -AsXml | 
            Select-Object -ExpandProperty TwilioResponse

       

    }
}