forked from tomtorggler/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-cap.ps1
121 lines (98 loc) · 4.27 KB
/
new-cap.ps1
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
function Get-AdObject {
param($identity)
if($identity -match "ou="){
New-Object -TypeName psobject -Property (@{DistinguishedName = "OU=b,DC=example,DC=com"})
}
}
function new-AdObject {
New-Object -TypeName psobject -Property (@{DistinguishedName = "CN=a,OU=b,DC=example,DC=com"})
}
function New-CAP {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[ValidateSet("EMEA-ES-ITV-ARB","EMEA-ES-ITV-URN")]
[string]
$Location,
[Parameter(Mandatory)]
[string]
$DisplayName,
[Parameter(Mandatory)]
[ValidatePattern("^\+\d+")]
[string]
$DisplayNumber,
[Parameter(Mandatory)]
[string]
$OU,
[Parameter(Mandatory)]
[int]
$PIN,
[Parameter()]
[int]
$ExtensionLength = 4
)
process {
$e164 = $DisplayNumber -replace "\+","" -replace " ",""
$Extension = $e164.Substring($e164.length -$ExtensionLength)
$ContactName = ($Location -replace "^\w4","PHONE"),$e164 -join "-"
$LineUri = "tel:+{0};ext={1}" -f $e164,$Extension
$SipUri = "sip:{0}@{1}.example.com" -f $ContactName,($Location.ToLower() -split "-" | Select-Object -First 1)
$ClientPolicy = $Location + "-CAP-STD"
$DialPlan = "tag:{0}" -f $Location
$VoicePolicy = $Location -replace "\w+$","International"
$Pool = switch -Regex ($Location) {
"EMEA-ES" { "es-bar-sbs1.example.com" }
"EMEA-DE" { "de-ber-sbs1.example.com" }
}
# learn how test-path works with ou
if($ContactObject = Get-AdObject $ContactName -ErrorAction SilentlyContinue){
Write-Verbose "Contact [$ContactName] found. Using existing object."
} elseif($OU = Get-Adobject -Identity $ou -ErrorAction SilentlyContinue) {
Write-Verbose "OU [$OU] exists"
Write-Verbose "Contact [$ContactName] does not exist."
if($PSCmdlet.ShouldProcess("Name [$ContactName]","Create Contact")){
$ContactObject = New-AdObject -Type Contact -Path $OU -Name $ContactName -PassThru
Write-Information "Created Contact: $($ContactObject.DistinguishedName)"
}
} else {
Write-Warning "Contact [$ContactName] not found, OU [$OU] not found. End of story."
continue
}
if($ContactObject){
Write-Verbose "Created Contact, starting jobs"
$InfoDict = @{
DN = $ContactObject.DistinguishedName
SipUri = $SipUri
LineUri = $LineUri
RegistrarPool = $Pool
DisplayName = $DisplayName
Pin = $Pin
DialPlan = $DialPlan
ClientPolicy = $ClientPolicy
VoicePolicy = $VoicePolicy
}
$InfoDict | ConvertTo-Json
if($PSCmdlet.ShouldProcess("dn [$($ContactObject.DistinguishedName)] pool [$pool] pin [$pin] cp [$ClientPolicy]","Create CAP")){
Start-Job -ArgumentList $InfoDict -ScriptBlock {
param($Input)
New-CsCommonAreaPhone @Input
Grant-CsDialPlan -Identity $Input.SipAddress -PolicyName $Input.DialPlan
Grant-CsVoicePolicy -Identity $Input.SipAddress -PolicyName $Input.VoicePolicy
Grant-CsClientPolicy -Identity $Input.SipAddress -PolicyName $Input.ClienPolicy
Set-CsClientPin -Identity $Input.SipAddress -PolicyName $Input.Pin
}
}
}
}
}
function Get-CAP {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateSet("EMEA-ES-ARB","EMEA-ES-URN")]
[string]
$Location
)
Get-CsCommonAreaPhone -Filter "DialPlan -eq $Location" | Select-Object -Property DisplayName,DisplayNumber,LineUri,SipAddress,DialPlan,VoicePolicy,ClienPolicy
}
#new-cap -Location EMEA-ES-ITV-ARB -DisplayName "itv arbizu test" -DisplayNumber "+34 123 456 789" -OU "ou=weur,dc=example,dc=com" -pin 123 -InformationAction Continue