Get-TwilioNumber.ps1

function Get-TwilioNumber
{
    <#
    .Synopsis
        Gets Twilio phone numbers
    .Description
        Gets numbers assoicated with a Twilio account, or finds a new number
    #>

    [CmdletBinding(DefaultParameterSetName='OwnedNumbers')]    
    param(        
    [Management.Automation.PSCredential]
    $Credential,

     # A setting storing the credential
    [Parameter(ValueFromPipelineByPropertyName=$true)]   
    [string[]]
    $Setting = @("TwilioAccountKey", "TwilioAccountSecret"),
    
    # If provided, will find an available phone number
    [Parameter(Mandatory=$true,ParameterSetName='AvailableNumbers')]
    [Switch]$Available,
    
    # If provided, will look for numbers near this phone number
    [Parameter(ParameterSetName='AvailableNumbers')]
    [string]$NearNumber,
    
    # Changes the radius of distance to look for a nearby number
    #|Default 25
    [Parameter(ParameterSetName='AvailableNumbers')]
    [Uint32]$Distance,
    
    # If set, will only find numbers near a latitude
    [Parameter(ParameterSetName='AvailableNumbers')]
    [string]$NearLatitude,
    
    # If set, will only find numbers near a longitude
    [Parameter(ParameterSetName='AvailableNumbers')]
    [string]$NearLongitude,
    
    # If set, will only find numbers in a specific rate center
    [Parameter(ParameterSetName='AvailableNumbers')]
    [string]$InRateCenter,
    
    # If set, will only find numbers in a specific LATA
    [Parameter(ParameterSetName='AvailableNumbers')]
    [string]$InLata,     

    # If provided, will find numbers only in a specific area code
    [Parameter(ParameterSetName='AvailableNumbers')]
    [Uint32]$AreaCode,     

    # If provided, will find numbers in an region (US and Canada only)
    [Parameter(ParameterSetName='AvailableNumbers')]
    [string]$InRegion,     

    # If set, will only find toll free numbers.
    [Parameter(ParameterSetName='AvailableNumbers')]
    [Switch]
    $TollFree,


    # If provided, will find numbers in a postal code (US and Canada only)
    [Parameter(ParameterSetName='AvailableNumbers')]
    [string]$InPostalCode,     
    
    # If set, will only find numbers containing a digit
    [Parameter(ParameterSetName='AvailableNumbers')]
    [Alias('ContainsDigit')]
    [string]$ContainDigit,
    
    # If set, will get confirmed outgoing numbers attached to an account
    [Parameter(Mandatory=$true,ParameterSetName='OutgoingNumbers')]
    [Alias('ConfirmedNumber', 'OutboundNumber')]
    [Switch]$OutGoingNumber,

    [Parameter(ParameterSetName='OutgoingNumbers')]    
    [Parameter(ParameterSetName='OwnedNumbers')]    
    [string]$PhoneNumber,

    [Parameter(ParameterSetName='OutgoingNumbers')]
    [Parameter(ParameterSetName='OwnedNumbers')]   
    [string]$FriendlyName


    )
    
    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

        }


        if (-not $Credential) {
            Write-Error "No Twilio Credential provided. Use -Credential or Set-TwilioCredential first"               
            return
        }
        
        if ($psCmdlet.ParameterSetName -eq 'OwnedNumbers') {
            $getWebParams = @{
                WebCredential=$Credential
                Url="https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/IncomingPhoneNumbers/"           
                AsXml =$true            
                UseWebRequest = $true
                
            }        

            if ($FriendlyName) {
                $GetWebParams.Url += "?FriendlyName=$FriendlyName"
            }
            if ($PhoneNumber) {
                $GetWebParams.Url += "?PhoneNumber=$PhoneNumber"
            }
            Get-Web @getwebParams |            
                Select-Object -ExpandProperty TwilioResponse | 
                Select-Object -ExpandProperty IncomingPhoneNumbers | 
                Select-Object -ExpandProperty IncomingPhoneNumber |
                ForEach-Object {
                    $_.pstypenames.clear()
                    $_.pstypenames.add('TwilioIncomingPhoneNumber')
                    $_
                }

        } elseif ($psCmdlet.ParameterSetName -eq 'AvailableNumbers') {
            $getWebParams = @{
                WebCredential=$Credential                
                AsXml =$true            
                UseWebRequest = $true
            }
            if (-not $TollFree) {
                $getWebParams.Url="https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/AvailablePhoneNumbers/$(((Get-Culture).ToString() -split "-")[-1])/Local?"           
            } else {
                $getWebParams.Url="https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/AvailablePhoneNumbers/$(((Get-Culture).ToString() -split "-")[-1])/TollFree?"           
            }
            
            
            
            if ($inLata) {
                $getWebParams.Url += "&InLata=$InLata"                        
            }

            if ($AreaCode) {
                $getWebParams.Url += "&AreaCode=$AreaCode"                        
            }

            if ($InRegion) {
                $getWebParams.Url += "&InRegion=$InRegion"                        
            }

            if ($InPostalCode) {
                $getWebParams.Url += "&InPostalCode=$InPostalCode"                        
            }
            
            if ($inRateCenter) {
                $getWebParams.Url += "&InRateCenter=$InRateCenter"                        
            }
            
            if ($NearNumber) {
                $getWebParams.Url += "&NearNumber=$NearNumber"                        
            }
            
            if ($Distance)  {
                $getWebParams.Url += "&Distance=$Distance"                        
            }
            
            if ($ContainDigit) {
                $getWebParams.Url += "&Contains=$ContainDigit"                        
            }
            
            $webResponse= Get-Web @getwebParams |            
                Select-Object -ExpandProperty TwilioResponse
            
            if ($webResponse.AvailablePhoneNumbers) { 
                $webResponse | 
                    Select-Object -ExpandProperty AvailablePhoneNumbers |
                    Where-Object {
                        $_.AvailablePhoneNumber 
                    } | 
                    Select-Object -ExpandProperty AvailablePhoneNumber |
                    ForEach-Object {
                        if ($_) { 
                            $_.pstypenames.clear()
                            $_.pstypenames.add('Twilio.AvailablePhoneNumber')
                            $_
                        }
                    }
                
            }
        } elseif ($psCmdlet.ParameterSetName -eq 'OutgoingNumbers') {
            $getWebParams = @{
                WebCredential=$Credential
                Url="https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/OutgoingCallerIds"           
                AsXml =$true            
                UseWebRequest = $true
            }        
            if ($FriendlyName) {
                $GetWebParams.Url += "?FriendlyName=$FriendlyName"
            }
            if ($PhoneNumber) {
                $GetWebParams.Url += "?PhoneNumber=$PhoneNumber"
            }
            Get-Web @getwebParams |            
                Select-Object -ExpandProperty TwilioResponse | 
                Select-Object -ExpandProperty OutgoingCallerIDs | 
                Select-Object -ExpandProperty OutgoingCallerID |
                ForEach-Object {
                    $_.pstypenames.clear()
                    $_.pstypenames.add('TwilioIncomingPhoneNumber')
                    $_
                }
        }

              
    }       
}