182 lines
5.1 KiB
PowerShell
182 lines
5.1 KiB
PowerShell
param(
|
|
[string]$Extensions = "201,205,206",
|
|
[string]$LogFile = ".\netgescon-tapi-com-events.log",
|
|
[int]$Seconds = 0
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$TapiMediaTypeAudio = 8
|
|
$TapiEventFilterCallNotification = 4
|
|
$TapiEventFilterCallState = 8
|
|
$TapiEventFilterCallMedia = 16
|
|
$TapiEventFilterCallHub = 32
|
|
$TapiEventFilterCallInfoChange = 64
|
|
$TapiEventFilterPrivate = 128
|
|
|
|
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 Write-LogLine {
|
|
param([string]$Text)
|
|
|
|
$line = "{0} | {1}" -f (Get-Date).ToString('yyyy-MM-dd HH:mm:ss'), $Text
|
|
Add-Content -Path $script:LogFilePath -Value $line -Encoding UTF8
|
|
Write-Host $line
|
|
}
|
|
|
|
function Try-RegisterCallNotifications {
|
|
param(
|
|
[Parameter(Mandatory = $true)] $Tapi,
|
|
[Parameter(Mandatory = $true)] $Address
|
|
)
|
|
|
|
try {
|
|
$cookie = $Tapi.RegisterCallNotifications($Address, $true, $true, $TapiMediaTypeAudio, 0)
|
|
return [pscustomobject]@{
|
|
Ok = $true
|
|
Cookie = $cookie
|
|
Error = ''
|
|
}
|
|
}
|
|
catch {
|
|
return [pscustomobject]@{
|
|
Ok = $false
|
|
Cookie = $null
|
|
Error = $_.Exception.Message
|
|
}
|
|
}
|
|
}
|
|
|
|
$script:LogFilePath = $LogFile
|
|
$watchList = @($Extensions.Split(',') | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne '' })
|
|
|
|
$tapi = New-Object -ComObject TAPI.TAPI
|
|
$tapi.Initialize()
|
|
$tapi.EventFilter = $TapiEventFilterCallNotification + $TapiEventFilterCallState + $TapiEventFilterCallMedia + $TapiEventFilterCallHub + $TapiEventFilterCallInfoChange + $TapiEventFilterPrivate
|
|
|
|
$allAddresses = Convert-ComCollectionToArray -Collection $tapi.Addresses
|
|
$addresses = @($allAddresses | Where-Object {
|
|
Should-WatchAddress -Address $_ -WatchList $watchList
|
|
})
|
|
|
|
Write-LogLine "=== AVVIO WATCH COM TAPI ==="
|
|
Write-LogLine "Watch list: $Extensions"
|
|
Write-LogLine "Provider addresses totali: $($allAddresses.Count)"
|
|
Write-LogLine "Provider addresses 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-LogLine ("ADDRESS READY name={0} dial={1} provider={2}" -f $addressName, $dial, $provider)
|
|
|
|
$registration = Try-RegisterCallNotifications -Tapi $tapi -Address $address
|
|
if ($registration.Ok) {
|
|
Write-LogLine ("REGISTER OK mode=monitor+owner cookie={0} address={1} dial={2}" -f $registration.Cookie, $addressName, $dial)
|
|
}
|
|
else {
|
|
Write-LogLine ("REGISTER KO address={0} dial={1} error={2}" -f $addressName, $dial, $registration.Error)
|
|
}
|
|
}
|
|
|
|
$sourceId = 'NetGescon.Tapi.Event'
|
|
try {
|
|
Unregister-Event -SourceIdentifier $sourceId -ErrorAction SilentlyContinue
|
|
} catch {}
|
|
|
|
$eventRegistration = Register-ObjectEvent -InputObject $tapi -EventName Event -SourceIdentifier $sourceId -Action {
|
|
$eventObject = $Event.SourceEventArgs
|
|
$messageData = $Event.MessageData
|
|
$logPath = $messageData.LogPath
|
|
|
|
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
|
$line = "{0} | COM EVENT type={1} argsType={2}" -f $timestamp, $eventObject.Event, $eventObject.GetType().FullName
|
|
Add-Content -Path $logPath -Value $line -Encoding UTF8
|
|
Write-Host $line
|
|
}
|
|
-MessageData @{ LogPath = $LogFile }
|
|
|
|
Write-LogLine "Sottoscrizione COM attiva. Fai ora chiamate reali su 201, 205 o 206."
|
|
|
|
if ($Seconds -gt 0) {
|
|
$end = (Get-Date).AddSeconds($Seconds)
|
|
while ((Get-Date) -lt $end) {
|
|
Wait-Event -SourceIdentifier $sourceId -Timeout 1 | Out-Null
|
|
}
|
|
}
|
|
else {
|
|
while ($true) {
|
|
Wait-Event -SourceIdentifier $sourceId -Timeout 1 | Out-Null
|
|
}
|
|
}
|