Jump to content

HOWTO: Get a list of free public numbers


Sebastian Dreier

Recommended Posts

  • Most Valued User

Hello everyone,

 

I am sure a lot of you already know the problem that it is not so easy to find out which public number is still available in a SwyxWare. Therefore I decided to create the following Powershell commandlet to solve this problem. This commandlet can be used with any version of the SwyxWare Powershell module. If you have questions feel free to ask :-)

 

<#
.SYNOPSIS
    Gets a list of all free public numbers including sip uris for all or one specific trunk.
.DESCRIPTION
    The Get-IpPbxFreePublicNumber cmdlet gets a list of all free public numbers including sip uris for all or one specific trunk.
.PARAMETER TrunkEntry
    Gets all free numbers for a specific trunk by using a trunk entry object.
.PARAMETER TrunkName
    Gets all free numbers for a specific trunk by using a trunk name (SQL wildcards are allowed, e.g. "%", "_").
.PARAMETER TrunkId
    Gets all free numbers for a specific trunk by using a unique trunk id.
.PARAMETER TrunkNumberRangeEntry
    Gets all free numbers for a specific trunk number range by using a trunk number range object.
.PARAMETER AddSIPURIs
    Adds SIP URIS to the list of free numbers.
.INPUTS
    [PortEntry] $TrunkGroupEntry
    [PortNumberRangeEntry] $TrunkNumberRangeEntry
.OUTPUTS
    System.Object[]
.EXAMPLE
    Get-IpPbxFreePublicNumber

    Returns a list of all free public number from all trunks
.EXAMPLE
    Get-IpPbxFreePublicNumber -AddSIPURIs

    Returns a list of all free public number including SIP URIs from all trunks
.EXAMPLE
    Get-IpPbxFreePublicNumber -TrunkName GateToHeaven

    Returns a list of all free public number from the trunk "GateToHeaven"
.EXAMPLE
    Get-IpPbxTrunk -TrunkName GateToHeaven | Get-IpPbxFreePublicNumber 

    Returns a list of all free public number from the trunk "GateToHeaven"
.EXAMPLE
    Get-IpPbxFreePublicNumber -TrunkId 1

    Returns a list of all free public number from the trunk with id 1
.EXAMPLE
    $trunkRanges = Get-IpPbxTrunkNumberRange -TrunkName GateToHeaven
    Get-IpPbxFreePublicNumber -TrunkNumberRangeEntry $trunkRanges[0]

    Returns a list of all free public number for the first number range from the trunk "GateToHeaven"
#>

function Get-IpPbxFreePublicNumber
{
    [CmdletBinding(DefaultParameterSetName='Default')]
    param([parameter(ParameterSetName='TrunkEntry', ValueFromPipeline=$true)][PortEntry]$TrunkEntry,
          [Parameter(ParameterSetName='TrunkName')][Alias("Name")][string]$TrunkName,
          [Parameter(ParameterSetName='TrunkId')][ValidateScript({($_ -gt 0)})][int]$TrunkId,
          [parameter(ParameterSetName='TrunkNumberRangeEntry', ValueFromPipeline=$true)][PortNumberRangeEntry]$TrunkNumberRangeEntry,
          [switch]$AddSIPURIs)
 

    BEGIN
    {
        $freeNumbers = @()
        $usedNumbers = @()
        $trunkNumberRanges  = @()
        
        Get-IpPbxPublicNumber | ForEach-Object { $usedNumbers += $_.Number }
    }
 

    PROCESS
    {
        # Check if we already have a trunk number range object
        if ($TrunkNumberRangeEntry)
        {
            $trunkNumberRanges += $TrunkNumberRangeEntry
        }
        else
        {
            # Get Trunk Entry object
            if ($TrunkName)
            {
                $TrunkEntry = Get-IpPbxTrunk -TrunkName $TrunkName
            }
            
            if ($TrunkId)
            {
                $TrunkEntry = Get-IpPbxTrunk -TrunkId $TrunkId
            }            
            
            # If the TrunkEntry variable is empty the 'Get-IpPbxTrunkNumberRange'
            # commadlet returns all number ranges from all trunks

            $trunkNumberRanges = Get-IpPbxTrunkNumberRange -TrunkEntry $TrunkEntry
        }
            
        # Get free numbers from all number ranges
        # I highly recommend the usage of the ForEach-Object commandlet
        # for ForEach loops. For more details please read the following article:
        # https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/08/getting-to-know-foreach-and-foreach-object/

        $trunkNumberRanges | ForEach-Object {
 

            # Save the current ForEach object in a separate variable.
            # This leads to better readability and maintainability.
            # Furthermore the separate variable can also be used
            # in antoher inner ForEach loop.

            $currentRange = $_
 

            # Check if this is a SIP URI
            if ($AddSIPURIs -and $currentRange.NumberType -eq "RangeTypSIPUri")
            {
                $sipuri = "$($currentRange.HeadNumber)$($currentRange.Realm)"

               

                # Ensure that the sip uri is free
                if ($usedNumbers -contains "sip:$($sipuri)") {
            
                    return
                }
 

                $freeNumbers += $sipuri
                return
            }
 

            # Ensure that this is a valid number range
            if (!$currentRange.TailNumberStart -or `
                !$currentRange.TailNumberEnd)
            {
                return 
            }  
            
            $currentRange.TailNumberStart..$currentRange.TailNumberEnd | ForEach-Object {
                    
                # Build number
                if ($currentRange.HeadNumber)
                {
                    $number = "+$($currentRange.CountryCode)$($currentRange.AreaCode)$($currentRange.HeadNumber)$($_)"
                }
                else
                {
                    $number = "+$($currentRange.CountryCode)$($currentRange.AreaCode)$($_)"
                }
            
                # Ensure that the number is free
                if ($usedNumbers -contains $number) {
            
                    return
                }
 

                $freeNumbers += $number
            }
        }
    }
 

    END
    {
        # Sort numbers and output
        $freeNumbers | Sort-Object
    }
}

 

 

Link to comment
Share on other sites


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and have taken note of our Privacy Policy.
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.