-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathAvailableTeamNumbersReport.ps1
197 lines (167 loc) · 7.42 KB
/
AvailableTeamNumbersReport.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
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
<#PSScriptInfo
.VERSION 2.0
.GUID be53af09-7831-40cc-92a2-0a72b3fa7c1b
.AUTHOR Vikas Sukhija
.COMPANYNAME Techwizard.cloud
.COPYRIGHT Techwizard.cloud
.TAGS
.LICENSEURI https://techwizard.cloud/2021/05/31/available-team-numbers-report/
.PROJECTURI https://techwizard.cloud/2021/05/31/available-team-numbers-report/
.ICONURI
.EXTERNALMODULEDEPENDENCIES MicrosoftTeams
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
https://techwizard.cloud/2021/05/31/available-team-numbers-report/
.PRIVATEDATA
#>
#Requires -Module MicrosoftTeams
<#
.DESCRIPTION
This will report the available phone numbers in Microsoft Teams
#>
param (
[string]$smtpserver,
[string]$erroremail,
[string]$from
)
###################Functions############################
function New-FolderCreation
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$foldername
)
$logpath = (Get-Location).path + "\" + "$foldername"
$testlogpath = Test-Path -Path $logpath
if($testlogpath -eq $false)
{
#Start-ProgressBar -Title "Creating $foldername folder" -Timer 10
$null = New-Item -Path (Get-Location).path -Name $foldername -Type directory
}
}
function Write-Log
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,ParameterSetName = 'Create')]
[array]$Name,
[Parameter(Mandatory = $true,ParameterSetName = 'Create')]
[string]$Ext,
[Parameter(Mandatory = $true,ParameterSetName = 'Create')]
[string]$folder,
[Parameter(ParameterSetName = 'Create',Position = 0)][switch]$Create,
[Parameter(Mandatory = $true,ParameterSetName = 'Message')]
[String]$message,
[Parameter(Mandatory = $true,ParameterSetName = 'Message')]
[String]$path,
[Parameter(Mandatory = $false,ParameterSetName = 'Message')]
[ValidateSet('Information','Warning','Error')]
[string]$Severity = 'Information',
[Parameter(ParameterSetName = 'Message',Position = 0)][Switch]$MSG
)
switch ($PsCmdlet.ParameterSetName) {
"Create"
{
$log = @()
$date1 = Get-Date -Format d
$date1 = $date1.ToString().Replace("/", "-")
$time = Get-Date -Format t
$time = $time.ToString().Replace(":", "-")
$time = $time.ToString().Replace(" ", "")
New-FolderCreation -foldername $folder
foreach ($n in $Name)
{$log += (Get-Location).Path + "\" + $folder + "\" + $n + "_" + $date1 + "_" + $time + "_.$Ext"}
return $log
}
"Message"
{
$date = Get-Date
$concatmessage = "|$date" + "| |" + $message +"| |" + "$Severity|"
switch($Severity){
"Information"{Write-Host -Object $concatmessage -ForegroundColor Green}
"Warning"{Write-Host -Object $concatmessage -ForegroundColor Yellow}
"Error"{Write-Host -Object $concatmessage -ForegroundColor Red}
}
Add-Content -Path $path -Value $concatmessage
}
}
} #Function Write-Log
#####################logs and reports###################
$log = Write-Log -Name "AvailableTeamNumbersReport-Log" -folder "logs" -Ext "log"
$Report1 = Write-Log -Name "AvailableTeamNumbersReport-Report" -folder "Report" -Ext "csv"
$collection = @()
######connect to Skob and import modules ###################################
Write-Log -message "Start..................Script" -path $log
try
{
Connect-MicrosoftTeams
Write-Log -Message "Connected to Teams module" -path $log
}
catch
{
$exception = $($_.Exception.Message)
Write-Log -Message "$exception" -path $log -Severity Error
Write-Log -Message "Exception has occured in connecting to Teams module" -path $log -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error occured in connecting to Teams module - AvailableTeamNumbersReport" -Body $($_.Exception.Message)
Exit;
}
##################start processing users##################
try
{
$allnumbers =Get-CsPhoneNumberAssignment -Top 100000 | Select TelephoneNumber,NumberType,ActivationState,City,IsoCountryCode,IsoSubdivision,PortInOrderStatus,PstnAssignmentStatus,PstnPartnerName
Write-Log -message "Fetched Phonenumbers $($allnumbers.count) from Teams" -path $log
$allassignednumbers = $allnumbers | where{$_.PstnAssignmentStatus -ne 'Unassigned'}
Write-Log -message "Fetched Assigned Phonenumbers $($allassignednumbers.count) from Teams" -path $log
$allunassignednumbers = $allnumbers | where{$_.PstnAssignmentStatus -eq 'Unassigned'}
Write-Log -message "Fetched unAssigned Phonenumbers $($allunassignednumbers.count) from Teams" -path $log
$getllcsonlineusernumbers = Get-CsOnlineUser -Filter {LineURI -ne $null} | Select UserprincipalName, LineURI
Write-Log -message "Fetched all assigned users $($getllcsonlineusernumbers.count) from Teams" -path $log
############## adding as error is not geteingr eported and less numbers are fetched#############
if($($allnumbers.count) -lt "8000"){
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error occured getting details from TEAMS AvailableTeamNumbersReport" -Body "Error occured getting details from TEAMS AvailableTeamNumbersReport"
exit
}
}
catch
{
$exception = $($_.Exception.Message)
Write-Log -Message "$exception" -path $log -Severity Error
Write-Log -Message "Exception has occured getting details from TEAMS" -path $log -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error occured getting details from TEAMS AvailableTeamNumbersReport" -Body $($_.Exception.Message)
Exit;
}
##########Export to Report#################################
Write-Log -Message "Start exporting Report" -path $log
[System.Collections.ArrayList]$collection = @()
ForEach($voicenumber in $allnumbers)
{
$mcoll = "" | Select-Object TelephoneNumber,TargetType,NumberType, ActivationState, City,IsoCountryCode,IsoSubdivision,PortInOrderStatus, PstnAssignmentStatus,PstnPartnerName
$mcoll.TelephoneNumber = $voicenumber.TelephoneNumber
$mcoll.NumberType = $voicenumber.NumberType
$mcoll.ActivationState = $voicenumber.ActivationState
$mcoll.City = $voicenumber.City
$mcoll.IsoCountryCode = $voicenumber.IsoCountryCode
$mcoll.IsoSubdivision = $voicenumber.IsoSubdivision
$mcoll.PortInOrderStatus = $voicenumber.PortInOrderStatus
$mcoll.PstnAssignmentStatus = $voicenumber.PstnAssignmentStatus
$mcoll.PstnPartnerName = $voicenumber.PstnPartnerName
if($voicenumber.PstnAssignmentStatus -eq "UserAssigned"){
$lineuri = $assigneduser = $null
$lineuri = "tel:" + $voicenumber.TelephoneNumber
$assigneduser = $getllcsonlineusernumbers | where{$_.LineURI -eq $lineuri} | select userprincipalname
$mcoll.TargetType = $assigneduser.userprincipalname
}
$collection.Add($mcoll) | out-null
}
Write-Log -Message "Data collected, export to CSV" -path $log
$collection | Export-Csv $Report1 -NoTypeInformation
Disconnect-MicrosoftTeams
##############################Recycle Logs##########################
Write-Log -Message "Recycle Logs" -path $log -Severity Information
Write-Log -message "Finish..................Script" -path $log
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Report - AvailableTeamNumbersReport" -Body "Report - AvailableTeamNumbersReport" -Attachments $report1
#############################################################################################