param( [string]$Extensions = "201,205,206" ) Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" $TapiMediaTypeAudio = 8 function Get-SafeProperty { param( [Parameter(Mandatory = $true)] $Object, [Parameter(Mandatory = $true)] [string]$Name ) try { return $Object.$Name } catch { return $null } } function Convert-ComCollectionToArray { param($Collection) $items = @() if ($null -eq $Collection) { return $items } try { foreach ($item in $Collection) { $items += $item } } catch { } return $items } function Normalize-ExtensionToken { param([string]$Value) if ($null -eq $Value) { return '' } $trimmed = ([string]$Value).Trim() if ($trimmed -eq '') { return '' } if ($trimmed -match '(\d+)$') { return $matches[1].TrimStart('0') } return $trimmed } function Should-WatchAddress { param( [Parameter(Mandatory = $true)] $Address, [Parameter(Mandatory = $true)] [string[]]$WatchList ) $dial = Normalize-ExtensionToken -Value ([string](Get-SafeProperty -Object $Address -Name 'DialableAddress')) $name = Normalize-ExtensionToken -Value ([string](Get-SafeProperty -Object $Address -Name 'AddressName')) foreach ($watch in $WatchList) { $token = Normalize-ExtensionToken -Value $watch if ($token -ne '' -and ($dial -eq $token -or $name -eq $token)) { return $true } } return $false } function Try-RegisterCallNotifications { param( [Parameter(Mandatory = $true)] $Tapi, [Parameter(Mandatory = $true)] $Address, [Parameter(Mandatory = $true)] [bool]$Monitor, [Parameter(Mandatory = $true)] [bool]$Owner ) try { $cookie = $Tapi.RegisterCallNotifications($Address, $Monitor, $Owner, $TapiMediaTypeAudio, 0) return [pscustomobject]@{ Ok = $true Cookie = $cookie HResult = '' Message = '' } } catch { $exception = $_.Exception return [pscustomobject]@{ Ok = $false Cookie = $null HResult = ('0x{0:X8}' -f ($exception.HResult -band 0xffffffff)) Message = $exception.Message } } } $watchList = @($Extensions.Split(',') | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne '' }) $tapi = New-Object -ComObject TAPI.TAPI $tapi.Initialize() $addresses = @(Convert-ComCollectionToArray -Collection $tapi.Addresses | Where-Object { Should-WatchAddress -Address $_ -WatchList $watchList }) Write-Host "Indirizzi filtrati: $($addresses.Count)" foreach ($address in $addresses) { $addressName = [string](Get-SafeProperty -Object $address -Name 'AddressName') $dial = [string](Get-SafeProperty -Object $address -Name 'DialableAddress') $provider = [string](Get-SafeProperty -Object $address -Name 'ServiceProviderName') Write-Host "" Write-Host ("=== {0} / {1} / {2} ===" -f $addressName, $dial, $provider) $tests = @( @{ Monitor = $true; Owner = $false; Label = 'monitor=true owner=false' }, @{ Monitor = $false; Owner = $true; Label = 'monitor=false owner=true' }, @{ Monitor = $true; Owner = $true; Label = 'monitor=true owner=true' } ) foreach ($test in $tests) { $result = Try-RegisterCallNotifications -Tapi $tapi -Address $address -Monitor $test.Monitor -Owner $test.Owner if ($result.Ok) { Write-Host ("OK {0} cookie={1}" -f $test.Label, $result.Cookie) } else { Write-Host ("FAIL {0} hresult={1} message={2}" -f $test.Label, $result.HResult, $result.Message) } } }