-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetSapiensAPI.psm1
1317 lines (1151 loc) · 38.1 KB
/
NetSapiensAPI.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using namespace System.Collections.Generic
using namespace System.Net.Http
using namespace System.Security
#Region Classes
class NSConnection {
[string]$BaseUrl
[string]$Token
[datetime]$TokenExpiration
[string]$ClientId
[string]$ClientSecret
hidden [PSCredential]$Credentials
hidden [Dictionary[string, object]]$Cache
NSConnection([string]$baseUrl, [string]$clientId, [string]$clientSecret) {
$this.BaseUrl = $baseUrl
$this.ClientId = $clientId
$this.ClientSecret = $clientSecret
$this.Cache = [Dictionary[string, object]]::new()
}
[void] Connect([PSCredential]$credentials) {
$this.Credentials = $credentials
$this.RefreshToken()
}
[void] RefreshToken() {
$tokenUrl = "{0}/ns-api/oauth2/token/?grant_type=password&client_id={1}&client_secret={2}&username={3}&password={4}" -f
$this.BaseUrl,
[Uri]::EscapeDataString($this.ClientId),
[Uri]::EscapeDataString($this.ClientSecret),
[Uri]::EscapeDataString($this.Credentials.UserName),
[Uri]::EscapeDataString($this.Credentials.GetNetworkCredential().Password)
$response = Invoke-RestMethod -Uri $tokenUrl -Method Post
$this.Token = $response.access_token
$this.TokenExpiration = (Get-Date).AddSeconds($response.expires_in)
}
[PSCustomObject] InvokeRequest([string]$object, [string]$action, [hashtable]$params) {
if (-not $this.Token -or $this.TokenExpiration -lt [datetime]::Now) {
$this.Authenticate()
}
# Build the query string
$queryParams = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
foreach ($key in $params.Keys) {
$queryParams[$key] = $params[$key]
}
$queryParams['object'] = $object
$queryParams['action'] = $action
$queryParams['format'] = 'json'
# Construct the URL with query parameters
$uri = "$($this.BaseUrl)/ns-api/?" + $queryParams.ToString()
$headers = @{
'Authorization' = "Bearer $($this.Token)"
}
try {
Write-Verbose "Requesting URL: $uri"
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
if ($response.PSObject.Properties.Name -contains 'code' -and $response.code -ne 200) {
Write-Warning "API returned error code $($response.code): $($response.msg)"
if ($response.code -eq 404) {
return $null
}
throw "API Error: $($response.msg)"
}
if ($response.PSObject.Properties.Name -contains 'total') {
return @{
total = [int]$response.total
items = $response.items
}
}
return $response
}
catch {
Write-Warning "API request failed: $_"
if ($_.Exception.Response.StatusCode -eq 404) {
return $null
}
throw
}
}
[void] ClearCache() {
$this.Cache.Clear()
}
}
class NSBaseObject {
[PSCustomObject]$RawData
NSBaseObject([PSCustomObject]$rawData) {
$this.RawData = $rawData
}
}
class NSDevice : NSBaseObject {
[string]$Domain
[string]$User
[string]$Mac
[string]$Model
[string]$Vendor
[string]$Extension
[string]$Suffix
[string]$FullExtension
[string]$AOR # Address of Record (SIP address) - Primary identifier for the device
# Additional subscriber-related fields
[string]$SubscriberName
[string]$SubscriberDomain
[string]$SubscriberEmail
[string]$AORUser
[string]$AuthenticationKey
[string]$SubscriberFullName
[string]$SubscriberLogin
[string]$SubscriberScope
[string]$ServiceCode
[string]$CallerID911
[string]$CallProcessingRule
[string]$Mode
[string]$UserAgent
[string]$Hostname
[string]$AddressIdSource
[string]$AddressPrettyPrint
[string]$Contact
[datetime]$RegistrationTime
[datetime]$RegistrationExpiresTime
NSDevice([PSCustomObject]$rawData) : base($rawData) {
$this.Domain = $rawData.subscriber_domain
$this.User = $rawData.subscriber_name
$this.Mac = $rawData.mac
$this.Model = $rawData.model
$this.Vendor = $rawData.vendor
$this.Extension = $rawData.subscriber_name
$rawData.aor -match '(sip:)(\d+)([a-z]{0,2})?@([\w\.\-]+)' | Out-Null
if($matches.count -gt 4) {
$this.FullExtension = $matches[2] + $matches[3]
$this.Suffix = $matches[3]
} elseif ($matches.count -gt 3) {
$this.FullExtension = $matches[2]
$this.Suffix = ""
}
$this.AOR = $rawData.aor # Primary identifier
# Map additional fields from raw data
$this.SubscriberName = $rawData.subscriber_name
$this.SubscriberDomain = $rawData.subscriber_domain
$this.AORUser = $rawData.aor_user
$this.AuthenticationKey = $rawData.authentication_key
$this.SubscriberFullName = $rawData.sub_fullname
$this.SubscriberLogin = $rawData.sub_login
$this.SubscriberScope = $rawData.sub_scope
$this.ServiceCode = $rawData.srvcode
$this.CallerID911 = $rawData.callid_emgr
$this.CallProcessingRule = $rawData.call_processing_rule
$this.Mode = $rawData.mode
$this.UserAgent = $rawData.user_agent
$this.Hostname = $rawData.hostname
$this.AddressIdSource = $rawData.address_id_source
$this.AddressPrettyPrint = $rawData.address_pretty_print
$this.Contact = $rawData.contact
# Parse datetime fields if they exist
if ($rawData.registration_time) {
$this.RegistrationTime = [datetime]::ParseExact($rawData.registration_time, "yyyy-MM-dd HH:mm:ss", $null)
}
if ($rawData.registration_expires_time) {
$this.RegistrationExpiresTime = [datetime]::ParseExact($rawData.registration_expires_time, "yyyy-MM-dd HH:mm:ss", $null)
}
}
[string] ToString() {
return $this.AOR
}
}
class NSSubscriber : NSBaseObject {
[string]$Domain
[string]$User
[string]$FirstName
[string]$LastName
[string]$FullName
[string]$Email
[string]$CallerIdName
[string]$CallerIdNumber
[string]$Scope
[bool]$VMailProvisioned
[string]$Site
[string]$Login
[string]$PIN
[string]$Timezone
[string]$VMailGreeting
[bool]$VMailEnabled
[string]$DialPlan
[string]$CallerID911
[string]$AreaCode
[string]$Presence
[bool]$DirList
[string]$ServiceCode
[string]$AccountStatus
[NSDevice[]]$Devices
NSSubscriber([PSCustomObject]$rawData) : base($rawData) {
$this.Domain = $rawData.domain
$this.User = $rawData.user
$this.FirstName = $rawData.first_name
$this.LastName = $rawData.last_name
$this.FullName = $($this.FirstName).Trim() + ' ' + $($this.LastName).Trim()
$this.Email = $rawData.email
$this.CallerIdName = $rawData.callid_name
$this.CallerIdNumber = $rawData.callid_nmbr
$this.Scope = $rawData.scope
$this.VMailProvisioned = ($rawData.vmail_provisioned -eq 'yes') -and $true -or ($rawData.vmail_provisioned -and $false)
$this.Site = $rawData.site
$this.Login = $rawData.subscriber_login
$this.PIN = $rawData.subscriber_pin
$this.Timezone = $rawData.time_zone
$this.VMailGreeting = $rawData.vmail_greeting
$this.VMailEnabled = ($rawData.vmail_enabled -eq 'yes') -and $true -or ($rawData.vmail_enabled -and $false)
$this.DialPlan = $rawData.dial_plan
$this.CallerID911 = $rawData.callid_emgr
$this.AreaCode = $rawData.area_code
$this.Presence = $rawData.presence
$this.DirList = ($rawData.dir_list -eq 'yes') -and $true -or ($rawData.dir_list -and $false)
$this.ServiceCode = $rawData.srv_code
$this.AccountStatus = $rawData.account_status
$this.Devices = @()
}
}
class NSAddress : NSBaseObject {
[string]$AddressId
[string]$Domain
[string]$Name
[string]$Description
[string]$Type
[string]$Status
[string]$PrettyPrint
[string]$Source
[string]$IpAddress
[bool]$IsEndpoint
[int]$ErrorCode
NSAddress([PSCustomObject]$rawData) : base($rawData) {
$this.AddressId = $rawData.address_id
$this.Domain = $rawData.domain
$this.Name = $rawData.address_name
$this.Description = $rawData.description
$this.Type = $rawData.type
$this.Status = $rawData.status
$this.PrettyPrint = $rawData.address_pretty_print
$this.Source = $rawData.address_id_source
$this.IpAddress = $rawData.address_ip
$this.IsEndpoint = [bool]::TryParse($rawData.is_endpoint_callid, [ref]$null) ? [bool]::Parse($rawData.is_endpoint_callid) : $false
$this.ErrorCode = [int]::TryParse($rawData.ndperror, [ref]$null) ? [int]::Parse($rawData.ndperror) : 0
}
[string] ToString() {
return "$($this.Name) ($($this.AddressId))"
}
}
class NSAddressEndpoint : NSBaseObject {
[string]$address_id
[string]$domain
[string]$address_name
[string]$caller_name
[string]$address_line_1
[string]$address_line_2
[string]$city
[string]$state_code
[string]$zip
[string]$country_code
[string]$location
[string]$public_ip
[string]$standardized
[string]$carrier
NSAddressEndpoint([PSCustomObject]$rawData) : base($rawData) {
$this.address_id = $rawData.address_id
$this.domain = $rawData.domain
$this.address_name = $rawData.address_name
$this.caller_name = $rawData.caller_name
$this.address_line_1 = $rawData.address_line_1
$this.address_line_2 = $rawData.address_line_2
$this.city = $rawData.city
$this.state_code = $rawData.state_code
$this.zip = $rawData.zip
$this.country_code = $rawData.country_code
$this.location = $rawData.location
$this.public_ip = $rawData.public_ip
$this.standardized = $rawData.standardized
$this.carrier = $rawData.carrier
}
[string] ToString() {
return "$($this.address_name) ($($this.address_id))"
}
}
class NSAgentLog : NSBaseObject {
[string]$AgentId
[string]$Domain
[string]$Action
[string]$Status
[datetime]$Timestamp
[string]$Details
NSAgentLog([PSCustomObject]$rawData) : base($rawData) {
$this.AgentId = $rawData.agent_id
$this.Domain = $rawData.domain
$this.Action = $rawData.action
$this.Status = $rawData.status
$this.Timestamp = [datetime]::TryParse($rawData.timestamp, [ref]$null) ? [datetime]::Parse($rawData.timestamp) : [datetime]::MinValue
$this.Details = $rawData.details
}
[string] ToString() {
return "[$($this.Timestamp)] $($this.Action): $($this.Status)"
}
}
class NSAnswerRule : NSBaseObject {
[string]$RuleId
[string]$Domain
[string]$Name
[string]$Description
[string]$Type
[string]$Pattern
[string]$Action
[bool]$Enabled
[int]$Priority
[datetime]$LastModified
NSAnswerRule([PSCustomObject]$rawData) : base($rawData) {
$this.RuleId = $rawData.rule_id
$this.Domain = $rawData.domain
$this.Name = $rawData.name
$this.Description = $rawData.description
$this.Type = $rawData.type
$this.Pattern = $rawData.pattern
$this.Action = $rawData.action
$this.Enabled = [bool]::TryParse($rawData.enabled, [ref]$null) ? [bool]::Parse($rawData.enabled) : $false
$this.Priority = [int]::TryParse($rawData.priority, [ref]$null) ? [int]::Parse($rawData.priority) : 0
$this.LastModified = [datetime]::TryParse($rawData.last_modified, [ref]$null) ? [datetime]::Parse($rawData.last_modified) : [datetime]::MinValue
}
[string] ToString() {
return "$($this.Name) ($($this.RuleId))"
}
}
class NSHuntGroup : NSBaseObject {
[string]$Name
[string]$Domain
[string]$Type
[string]$Status
[string]$Description
NSHuntGroup([PSCustomObject]$rawData) : base($rawData) {
$this.Name = $rawData.huntgroup_name
$this.Domain = $rawData.domain
$this.Type = $rawData.type
$this.Status = $rawData.status
$this.Description = $rawData.description
}
[string] ToString() {
return $this.Name
}
}
class NSCallQueue : NSBaseObject {
[string]$queue_name
[string]$domain
[string]$description
[string]$default_user
[string]$queue_option
[string]$max_time
[string]$wait_limit
[string]$length_limit
[string]$agent_required
[string]$callback_max_hours
[string]$queuedcall_count
[string]$agent_count
[string]$huntgroup_option
[string]$connect_to
[string]$run_stats
[string]$sring_1st
[string]$sring_inc
[string]$auto_logout
NSCallQueue([PSCustomObject]$rawData) : base($rawData) {
$this.queue_name = $rawData.queue_name
$this.domain = $rawData.domain
$this.description = $rawData.description
$this.default_user = $rawData.default_user
$this.queue_option = $rawData.queue_option
$this.max_time = $rawData.max_time
$this.wait_limit = $rawData.wait_limit
$this.length_limit = $rawData.length_limit
$this.agent_required = $rawData.agent_required
$this.callback_max_hours = $rawData.callback_max_hours
$this.queuedcall_count = $rawData.queuedcall_count
$this.agent_count = $rawData.agent_count
$this.huntgroup_option = $rawData.huntgroup_option
$this.connect_to = $rawData.connect_to
$this.run_stats = $rawData.run_stats
$this.sring_1st = $rawData.sring_1st
$this.sring_inc = $rawData.sring_inc
$this.auto_logout = $rawData.auto_logout
}
[string] ToString() {
return "$($this.queue_name) ($($this.description))"
}
}
class NSQueueAgent : NSBaseObject {
[string]$device_aor
[string]$huntgroup_name
[string]$huntgroup_domain
[string]$entry_option
[string]$wrap_up_sec
[string]$auto_ans
[string]$entry_order
[string]$entry_priority
[string]$call_limit
[string]$confirm_required
[string]$entry_device
[string]$entry_status
[string]$owner_user
[string]$owner_domain
[string]$session_count
[string]$error_info
[string]$last_update
[string]$device
[string]$stat
[string]$sub_firstname
[string]$sub_lastname
[string]$sub_login
[string]$sub_fullname
NSQueueAgent([PSCustomObject]$rawData) : base($rawData) {
$this.device_aor = $rawData.device_aor
$this.huntgroup_name = $rawData.huntgroup_name
$this.huntgroup_domain = $rawData.huntgroup_domain
$this.entry_option = $rawData.entry_option
$this.wrap_up_sec = $rawData.wrap_up_sec
$this.auto_ans = $rawData.auto_ans
$this.entry_order = $rawData.entry_order
$this.entry_priority = $rawData.entry_priority
$this.call_limit = $rawData.call_limit
$this.confirm_required = $rawData.confirm_required
$this.entry_device = $rawData.entry_device
$this.entry_status = $rawData.entry_status
$this.owner_user = $rawData.owner_user
$this.owner_domain = $rawData.owner_domain
$this.session_count = $rawData.session_count
$this.error_info = $rawData.error_info
$this.last_update = $rawData.last_update
$this.device = $rawData.device
$this.stat = $rawData.stat
$this.sub_firstname = $rawData.sub_firstname
$this.sub_lastname = $rawData.sub_lastname
$this.sub_login = $rawData.sub_login
$this.sub_fullname = $rawData.sub_fullname
}
[string] ToString() {
return "$($this.sub_fullname) ($($this.device_aor))"
}
}
#EndRegion
#Region Variables
$script:NSConnection = $null
#EndRegion
#Region Public Functions
function Connect-NSServer {
<#
.SYNOPSIS
Connects to the NetSapiens API
.DESCRIPTION
Establishes a connection to the NetSapiens API using the provided credentials and connection details
.PARAMETER BaseUrl
The base URL of the NetSapiens API (e.g., https://api.ucaasnetwork.com)
.PARAMETER ClientId
The client ID for API authentication
.PARAMETER ClientSecret
The client secret for API authentication
.PARAMETER Credential
PSCredential object containing username and password
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$BaseUrl,
[Parameter(Mandatory = $true)]
[string]$ClientId,
[Parameter(Mandatory = $true)]
[string]$ClientSecret,
[Parameter(Mandatory = $true)]
[PSCredential]$Credential
)
$script:NSConnection = [NSConnection]::new($BaseUrl, $ClientId, $ClientSecret)
$script:NSConnection.Connect($Credential)
}
function Get-NSSubscriber {
<#
.SYNOPSIS
Gets subscriber information from NetSapiens
.DESCRIPTION
Retrieves subscriber details including devices from the NetSapiens API
.PARAMETER Domain
The domain to search in
.PARAMETER User
Optional user to filter by
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Domain,
[Parameter(Mandatory = $false)]
[string]$User
)
$params = @{
domain = $Domain
}
if ($User) {
$params['user'] = $User
}
$response = $script:NSConnection.InvokeRequest('subscriber', 'read', $params)
$subscribers = @()
foreach ($sub in $response) {
$subscriber = [NSSubscriber]::new($sub)
# Get devices for this subscriber
$devices = Get-NSDevice -Domain $Domain -User $sub.user
$subscriber.Devices = $devices
$subscribers += $subscriber
}
return $subscribers
}
function Get-NSDevice {
<#
.SYNOPSIS
Retrieves device details from NetSapiens
.DESCRIPTION
Retrieves device details for a specific domain and optionally a specific user.
Each device is uniquely identified by its AOR (Address of Record/SIP address).
.PARAMETER Domain
The domain to search in
.PARAMETER User
Optional user to filter devices by
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Domain,
[Parameter(Mandatory = $false)]
[string]$User
)
$params = @{
object = "device"
action = "read"
domain = $Domain
format = "json"
}
if ($User) {
$params['user'] = $User
}
$response = $script:NSConnection.InvokeRequest('device', 'read', $params)
$devices = @()
foreach ($dev in $response) {
$device = [NSDevice]::new($dev)
$devices += $device
}
return $devices
}
function New-NSDevice {
<#
.SYNOPSIS
Creates a new device in NetSapiens if it doesn't already exist
.DESCRIPTION
Creates a new device in NetSapiens with the specified parameters.
The Device parameter (SIP address) serves as the unique identifier for the device.
The Device parameter should be in the format "sip:####[a-z]@domain.com"
where #### is the user's extension and [a-z] is an optional lowercase letter.
.PARAMETER Domain
The domain name for the device (e.g., "domain.service")
.PARAMETER Device
The full SIP address for the device (e.g., "sip:1234a@domain.service").
This serves as the unique identifier for the device.
.PARAMETER User
The user extension (3-4 digits)
.PARAMETER Mac
The MAC address of the device (can be empty)
.PARAMETER Model
The model of the device (can be empty)
.PARAMETER Type
Optional device type (e.g., "SIP")
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Domain,
[Parameter(Mandatory = $true)]
[string]$Device,
[Parameter(Mandatory = $true)]
[string]$User,
[Parameter(Mandatory = $false)]
[string]$Mac = "",
[Parameter(Mandatory = $false)]
[string]$Model = "",
[Parameter(Mandatory = $false)]
[string]$Type = ""
)
begin {
$validationErrors = @()
}
process {
try {
# Input validation
if ($Domain.Length -lt 4) {
Add-ValidationError "Domain name must be at least 4 characters long."
}
if (-not ($User -match '^\d{3,4}$')) {
Add-ValidationError "User must be a 3 or 4 digit number."
}
# Validate Device SIP address format
if (-not ($Device -match '(sip:)(\d+)([a-z]{0,2})?@([\w\.\-]+)')) {
Add-ValidationError "Invalid Device SIP address format. Must be like 'sip:1234a@domain.com'"
}
else {
if ($matches[1] -ne "sip:") {
Add-ValidationError "Device SIP address must start with 'sip:'"
}
if (-not ($matches[2] -match '^\d{3,4}$')) {
Add-ValidationError "Device SIP username must be 3 or 4 digits"
}
if ($matches[3] -and -not ($matches[3] -match '^[a-z]{0,2}$')) {
Add-ValidationError "Device SIP designator must be empty or a single lowercase letter"
}
}
if ($Mac -and -not ($Mac -match '^([0-9A-Fa-f]{12})$')) {
Add-ValidationError "Invalid MAC address format. Use format: 001122334455"
}
# Check for validation errors
if ($validationErrors.Count -gt 0) {
throw "Validation failed:`n$($validationErrors -join "`n")"
}
Write-Verbose "Checking for existing device with SIP address: $Device"
$existingDevices = Get-NSDevice -Domain $Domain
$existingDevice = $existingDevices | Where-Object { $_.AOR -eq $Device }
if ($existingDevice) {
Write-Verbose "Device with SIP address $Device already exists. Returning existing device."
return $existingDevice
}
Write-Verbose "Creating new device..."
$params = @{
domain = $Domain
device = $Device
user = $User
mac = $Mac
model = $Model
}
if ($Type) {
$params['type'] = $Type
}
$response = $script:NSConnection.InvokeRequest('device', 'create', $params)
# Give the API a moment to process
Start-Sleep -Milliseconds 500
# Verify the device was created
$newDevice = Get-NSDevice -Domain $Domain | Where-Object { $_.AOR -eq $Device }
if (-not $newDevice) {
throw "Device creation failed. Device not found after creation."
}
return $newDevice
}
catch {
$errorMessage = "Failed to create device: $($_.Exception.Message)"
Write-Error $errorMessage
throw $errorMessage
}
}
}
function Get-NSAddress {
<#
.SYNOPSIS
Retrieves address information from NetSapiens
.DESCRIPTION
Gets address details for a specific domain and optionally filters by address ID or name
.PARAMETER Domain
The domain to search in
.PARAMETER AddressId
Optional address ID to filter by
.PARAMETER Name
Optional address name to filter by
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Domain,
[Parameter(Mandatory = $false)]
[string]$AddressId,
[Parameter(Mandatory = $false)]
[string]$Name
)
$params = @{
object = "address"
action = "read"
domain = $Domain
format = "json"
}
if ($AddressId) {
$params['address_id'] = $AddressId
}
if ($Name) {
$params['address_name'] = $Name
}
$response = $script:NSConnection.InvokeRequest('address', 'read', $params)
$addresses = @()
foreach ($addr in $response) {
$address = [NSAddress]::new($addr)
$addresses += $address
}
return $addresses
}
function Get-NSAddressCount {
<#
.SYNOPSIS
Gets the count of addresses in a domain
.DESCRIPTION
Returns the total number of addresses in the specified domain
.PARAMETER Domain
The domain to count addresses in
.PARAMETER AddressId
Optional address ID to filter by
.PARAMETER Name
Optional address name to filter by
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Domain,
[Parameter(Mandatory = $false)]
[string]$AddressId,
[Parameter(Mandatory = $false)]
[string]$Name
)
$params = @{
object = "address"
action = "count"
domain = $Domain
format = "json"
}
if ($AddressId) {
$params['address_id'] = $AddressId
}
if ($Name) {
$params['address_name'] = $Name
}
$response = $script:NSConnection.InvokeRequest('address', 'count', $params)
return [int]$response.total
}
function New-NSAddress {
<#
.SYNOPSIS
Creates a new address in NetSapiens
.DESCRIPTION
Creates a new address with the specified parameters
.PARAMETER Domain
The domain for the address
.PARAMETER Name
The name of the address
.PARAMETER Type
The type of address
.PARAMETER Description
Optional description for the address
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Domain,
[Parameter(Mandatory = $true)]
[string]$Name,
[Parameter(Mandatory = $true)]
[string]$Type,
[Parameter(Mandatory = $false)]
[string]$Description = ""
)
$params = @{
object = "address"
action = "create"
domain = $Domain
address_name = $Name
type = $Type
format = "json"
}
if ($Description) {
$params['description'] = $Description
}
$response = $script:NSConnection.InvokeRequest('address', 'create', $params)
return Get-NSAddress -Domain $Domain -AddressId $response.address_id
}
function Remove-NSAddress {
<#
.SYNOPSIS
Deletes an address from NetSapiens
.DESCRIPTION
Removes the specified address from the system
.PARAMETER Domain
The domain of the address
.PARAMETER AddressId
The ID of the address to delete
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Domain,
[Parameter(Mandatory = $true)]
[string]$AddressId
)
$params = @{
object = "address"
action = "delete"
domain = $Domain
address_id = $AddressId
format = "json"
}
$script:NSConnection.InvokeRequest('address', 'delete', $params)
}
function Copy-NSAddressEndpoint {
<#
.SYNOPSIS
Copies an endpoint address to another domain
.DESCRIPTION
Creates a copy of an endpoint address in a different domain
.PARAMETER OriginalDomain
The source domain
.PARAMETER TargetDomain
The target domain
.PARAMETER AddressId
The ID of the address to copy
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$OriginalDomain,
[Parameter(Mandatory = $true)]
[string]$TargetDomain,
[Parameter(Mandatory = $true)]
[string]$AddressId
)
$params = @{
object = "address"
action = "copyEndpoint"
original_domain = $OriginalDomain
to_domain = $TargetDomain
original_address_id = $AddressId
format = "json"
}
$response = $script:NSConnection.InvokeRequest('address', 'copyEndpoint', $params)
return Get-NSAddress -Domain $TargetDomain -AddressId $response.address_id
}
function Get-NSAddressEndpoint {
<#
.SYNOPSIS
Gets address endpoints from NetSapiens
.DESCRIPTION
Returns a list of address endpoints in the specified domain
.PARAMETER Domain
The domain to get address endpoints from
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Domain
)
$params = @{
domain = $Domain
carrier = 'bandwidth' # Always set to 'bandwidth' as required
}
Write-Verbose "Getting address endpoints for domain: $Domain"
$response = $script:NSConnection.InvokeRequest('address', 'readAddressEndpoint', $params)
Write-Verbose "Address endpoint response: $($response | ConvertTo-Json -Depth 10)"
if ($null -eq $response) {
Write-Verbose "No address endpoints found"
return @()
}
if ($response -is [System.Collections.IEnumerable] -and $response -isnot [string]) {
Write-Verbose "Converting multiple address endpoints"
return $response | ForEach-Object { [NSAddressEndpoint]::new($_) }
}
else {
Write-Verbose "Converting single address endpoint"
return @([NSAddressEndpoint]::new($response))
}
}
function Update-NSAddress {
<#
.SYNOPSIS
Updates an existing address
.DESCRIPTION
Modifies the properties of an existing address
.PARAMETER Domain
The domain of the address
.PARAMETER AddressId
The ID of the address to update
.PARAMETER Name
Optional new name for the address
.PARAMETER Type
Optional new type for the address
.PARAMETER Description
Optional new description for the address
#>
[CmdletBinding()]