-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArchiveTeamsChannelConversation.ps1
138 lines (101 loc) · 4.96 KB
/
ArchiveTeamsChannelConversation.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
### Team conversation archiver ###
### Version 1.0 ###
### Author: Alexander Holmeset ###
### Twitter: twitter.com/alexholmeset ###
### Blog: alexholmeset.blog ###
#Description:
#You specify a Group/team object ID, then the script archives all conversations in every channel for this team in a HTML file.
#Have in mind information protection policies like GDPR when working on information like this.
#Parameters
#
$resourceURI = "https://graph.microsoft.com"
$authority = "https://login.microsoftonline.com/common"
#Enter your own clientID and redirect URI.Take a look at Ståle Hansen blogpost to see how to create your own clientid and redirect URI.
#https://msunified.net/2018/12/12/post-at-microsoftteams-channel-chat-message-from-powershell-using-graph-api/
#
$clientId = "3587e50e-98f4-4bbe-86ff-f94c2056a7e8"
$redirectUri = "https://login.microsoftonline.com/M365x792147.onmicrosoft.com/oauth2"
#Remove commenting on username and password if you want to run this without a prompt.
#$Office365Username='user@domain'
#$Office365Password='VeryStrongPassword'
#pre requisites
try {
$AadModule = Import-Module -Name AzureAD -ErrorAction Stop -PassThru
}
catch {
throw 'Prerequisites not installed (AzureAD PowerShell module not installed)'
}
$adal = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
##option without user interaction
if (([string]::IsNullOrEmpty($Office365Username) -eq $false) -and ([string]::IsNullOrEmpty($Office365Password) -eq $false))
{
$SecurePassword = ConvertTo-SecureString -AsPlainText $Office365Password -Force
#Build Azure AD credentials object
$AADCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserPasswordCredential" -ArgumentList $Office365Username,$SecurePassword
# Get token without login prompts.
$authResult = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions]::AcquireTokenAsync($authContext, $resourceURI, $clientid, $AADCredential);
}
else
{
# Get token by prompting login window.
$platformParameters = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" -ArgumentList "Always"
$authResult = $authContext.AcquireTokenAsync($resourceURI, $ClientID, $RedirectUri, $platformParameters)
}
$accessToken = $authResult.result.AccessToken
#Group/team object ID.
$TeamID = 'f8e68301-8d91-4360-b033-eeb78ca2077f'
#Where to store the HTML file:
$Storage = 'c:\temp\test.html'
#Gets all channels in a Team
$apiUrl = "https://graph.microsoft.com/beta/teams/$TeamID/channels"
$myProfile = Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $apiUrl -Method Get
$TeamChannels = $myprofile.value | Select-Object ID,DisplayName
"
" | Out-File $Storage
foreach($Channel in $TeamChannels) {
#Gets all root messages/conversations in a channel.
$apiUrl = "https://graph.microsoft.com/beta/teams/$TeamID/channels/"+$channel.id+"/messages"
$myProfile = Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $apiUrl -Method Get
$ChannelMessages = $myprofile.value | Select-Object Body,From,ID,attachments,createdDateTime | Sort-Object
$Channeldisplayname = $channel.displayName
"<br>
------------------------------<br>
<br>
$channeldisplayname<br>
<br>" | Out-File -Append $Storage
foreach($channelmessage in $ChannelMessages){
$channelmessagedisplayname = (($channelmessage.from).user).displayname
$channelmessagecontent = ($channelmessage.body).content
$channelmessageattachment = ($channelmessage.attachments).contenturl
$ChannelmessagecreatedDateTime = $channelmessage.createdDateTime
"<br>
***************************<br>
$channelmessagecreatedDateTime<br>
$channelmessagedisplayname<br>
$channelmessagecontent<br>
$channelmessageattachment<br>
<br>" | out-file -Append $Storage
#Gets all replies to a root message in a channel.
$apiUrl = "https://graph.microsoft.com/beta/teams/$TeamID/channels/"+$channel.id+"/messages/"+$ChannelMessage.id+"/replies"
$myProfile = Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $apiUrl -Method Get
$replies = $myprofile.value | Select-Object body,from,attachments,createdDateTime | Sort-Object
foreach($reply in $replies){
$replydisplayname = (($reply.from).user).displayname
$replycontent = ($reply.body).content
$replyattachment = ($reply.attachments).contenturl
$replycreatedDateTime = $reply.createdDateTime
"<br>
$replycreatedDateTime<br>
$replydisplayname<br>
$replycontent<br>
$replyattachment<br>
<br>" | out-file -Append $Storage
}
}
}