Skip to content

Instantly share code, notes, and snippets.

@PierrickBIT
Created January 31, 2023 07:36
Show Gist options
  • Save PierrickBIT/953091bb420277a73a1d7b583e268fe3 to your computer and use it in GitHub Desktop.
Save PierrickBIT/953091bb420277a73a1d7b583e268fe3 to your computer and use it in GitHub Desktop.
[MS TEAMS] Copier les fichiers d'une conversation vers une bibliothèque SharePoint 365
# Charger les bibliothèques requises
Install-Module Microsoft.Graph.Teams -Force
Install-Module Microsoft.Graph.SharePoint -Force
# Définir les informations d'identification de l'application
$clientId = "<Application ID>"
$clientSecret = "<Application Secret>"
$tenantId = "<Tenant ID>"
# Obtenir un jeton d'accès
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/$tenantId"
$clientCred = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $clientId, $clientSecret
$authResult = $authContext.AcquireTokenAsync("https://graph.microsoft.com", $clientCred).Result
# Définir les informations de la conversation dans Microsoft Teams
$teamId = "<Team ID>"
$channelId = "<Channel ID>"
$conversationId = "<Conversation ID>"
# Obtenir la liste des fichiers dans la conversation
$files = Invoke-RestMethod -Headers @{Authorization = "Bearer $($authResult.AccessToken)"} -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels/$channelId/messages/$conversationId/attachments"
# Boucle sur chaque fichier pour le télécharger et le copier vers la bibliothèque SharePoint
foreach ($file in $files.value) {
# Télécharger le fichier
$fileContent = Invoke-RestMethod -Method Get -Headers @{Authorization = "Bearer $($authResult.AccessToken)"} -Uri $file.contentUrl
# Copier le fichier vers la bibliothèque SharePoint
$fileStream = [System.IO.MemoryStream][System.Convert]::FromBase64String($fileContent)
$fileName = $file.Name
$siteId = "<SharePoint Site ID>"
$webId = "<SharePoint Web ID>"
$listId = "<SharePoint List ID>"
# Copier le fichier vers la bibliothèque SharePoint
Invoke-RestMethod -Method Post -Headers @{Authorization = "Bearer $($authResult.AccessToken)"} -Uri "https://graph.microsoft.com/v1.0/sites/$siteId/lists/$listId/items/$fileName/content" -InFile $fileStream
}
@guillaumemeyer
Copy link

Ca m'a l'air bon, même si je ne suis pas un pro PowerShell.
Pour rendre ce script plus résilient, j'ajouterais un test des permissions associées a l'access token une fois authentifié, en decodant le JWT, car ce script dépend de plusieurs permissions, comme Chat.Read.All.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment