TeamsReport.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
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

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 2d8f6138-efd3-4b5e-8a13-8e18a25fff43
 
.AUTHOR Vikas Sukhija
 
.COMPANYNAME SysCloudpro.com
 
.COPYRIGHT @SysCloudpro.com
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 This script will extract HTML report on all Teams across the Tenant
 Detail documentation : https://syscloudpro.com/2019/04/07/microsoft-teams-report-for-office-365-tenant/
 
#>
 
############Script Parameters##############
param (
  [string]$userId = $(Read-host "Enter UserID"), 
  $password = $(Read-host "Enter Password"-AsSecureString)
)
#############Load Functions#################
$error.clear()
try { $null = Stop-Transcript }
catch { $error.clear() }

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(" ", "")
    
      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 $concatmessage -ForegroundColor Green}
        "Warning"{Write-Host $concatmessage -ForegroundColor Yellow}
        "Error"{Write-Host $concatmessage -ForegroundColor Red}
      }
      
      Add-Content -Path $path -Value $concatmessage
    }
  }
}

function LaunchEOL
{
  param
  (
    $cred
  )
    
  $UserCredential = $cred
    
  $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    
  Import-PSSession $Session -Prefix "EOL" -AllowClobber
}
Function RemoveEOL
{
  $Session = Get-PSSession | where { $_.ComputerName -like "outlook.office365.com" }
  Remove-PSSession $Session
}
##########################Load variables & Logs####################
$log = Write-Log -Name "MSTeamOwnerReport" -folder logs -Ext log
$output1 = Write-Log -Name "MSTeamOwner" -folder Report -Ext html

$smtpserver = "SMTPserver"
$from = "donotreply@labtest.com"
$erroremail = "Reports@labtest.com"
$collection =@()

#####################Create folder and credential from arguments###############
$logpath  = (Get-Location).path + "\logs" 
$testlogpath = Test-Path -Path $logpath
if($testlogpath -eq $false)
{New-Item -Path (Get-Location).path -Name Logs -Type directory}

$reportpath = (Get-Location).path + "\Report"
$testReportpath = Test-Path -Path $reportpath
if($testReportpath -eq $false)
{New-Item -Path (Get-Location).path -Name Report -Type directory}

$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $userId, $password
##########Start Script main##############

Write-Log -Message "Script....Started" -path $log
try
{
  Connect-MicrosoftTeams -Credential $Credential
  Write-Log -Message "Connected to teams Module" -path $log
}
catch
{
  $exception = $($_.Exception.Message)
  Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error Connecting MS Team Module MS Team Report" -Body $($_.Exception.Message)
  Write-Log -Message "Exception occured connecting Teams module $exception" -path $log -Severity Error
  Exit
}
try
{
  Write-Host "Connecting to EOL" -ForegroundColor Green
  LaunchEOL -cred $Credential
  Write-Log -Message "Connected to Exchange Online" -path $log
}
catch
{
  $exception = $($_.Exception.Message)
  Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error - EOL Connection MS Team Report" -Body $($_.Exception.Message)
  Write-Log -Message "Exception occured connecting EOL Shell $exception" -path $log -Severity Error
  Exit
}

$collTeam = Get-Team | select DisplayName, GroupId, Description
Write-Log -Message "feteched All Teams" -path $log
Disconnect-MicrosoftTeams
$collUnifiedGroup = get-EOLunifiedgroup -resultsize unlimited | select Name, AccessType, ExternalDirectoryObjectId, WhenCreated, ManagedByDetails, SharePointSiteUrl
Write-Log -Message "feteched All Unified Groups" -path $log
RemoveEOL
Foreach($team in $collTeam)
{
  $mcoll = "" | select Name, Description, GroupId, AccessType, WhenCreated, SharePointSiteUrl, ManagedByDetails
  $teamdispname = $team.DisplayName
  Write-Log -Message "Processing.............$teamdispname" -path $log
  $mcoll.Name = $team.DisplayName
  $mcoll.Description = $team.Description
  $mcoll.GroupId = $team.groupId
  foreach($ugroup in $collUnifiedGroup)
  {
    if($team.GroupId -eq $ugroup.ExternalDirectoryObjectId)
    {
      $mcoll.AccessType = $ugroup.AccessType
      $mcoll.WhenCreated = $ugroup.WhenCreated
      $mcoll.SharePointSiteUrl = $ugroup.SharePointSiteUrl
      $mcoll.ManagedByDetails = $ugroup.ManagedByDetails
    }
  }
  
  $collection += $mcoll
}

############Format HTML###########
$HTMLFormat = "<style>"
$HTMLFormat = $HTMLFormat + "BODY{background-color:GainsBoro;}"
$HTMLFormat = $HTMLFormat + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$HTMLFormat = $HTMLFormat + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:darksalmon}"
$HTMLFormat = $HTMLFormat + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:LightBlue}"
$HTMLFormat = $HTMLFormat + "</style>"
################################
$count = $collection.count
$privateteams = $collection | where{$_.AccessType -eq "Private"}
$publicteams = $collection | where{$_.AccessType -eq "Public"}
$privateteamscount = $privateteams.count
$publicteamscount = $publicteams.count

Write-Log -Message "Converting to HTML" -path $log
$collection |
ConvertTo-Html -Head $HTMLFormat -Body "<H2><Font Size = 4,Color = DarkCyan>Microsoft Teams = $count, Private Teams = $privateteamscount, Public Teams = $publicteamscount </Font></H2>" -AS Table |
Set-Content $output1

Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "MS Team Report" -Body "MS Team Report" -Attachments $output1
Write-Log -Message "Report Sent to $erroremail" -path $log
########################Recycle reports & logs##############
$path1 = (Get-Location).path + "\report"
$path2 = (Get-Location).path + "\Logs"
$limit = (Get-Date).AddDays(-60) #for report recycling
Get-ChildItem -Path $path1 |
Where-Object -FilterScript {$_.CreationTime -lt $limit} |
Remove-Item -Recurse -Force

Get-ChildItem -Path $path2 |
Where-Object -FilterScript {$_.CreationTime -lt $limit} |
Remove-Item -Recurse -Force
Write-Log -Message "Script....Finished" -path $log
##############################################################################