Clear-MSTeamsCache/Clear-MSTeamsCache.ps1

95 lines
3.3 KiB
PowerShell
Raw Normal View History

<#
.SYNOPSIS
Nettoyage du cache de Microsoft Teams et des Credentials compte Microsoft pour le module Microsoft.AAD.BrokerPlugin
A lancer en admin
Peut nécessiter de lancer en désactivant les restrictions liées à la politique d'exécution des scripts sur certains PC au préalable :
Set-ExecutionPolicy Bypass -Scope Process
.DESCRIPTION
Suppression des fichiers de cache de MS Teams et des credentials du module Microsoft.AAD.BrokerPlugin
.PARAMETER <Parameter_Name>
-SamAccountName
.INPUTS
Aucun
.OUTPUTS
Aucun
.NOTES
Version: 0.2
Author: Simon404
Creation Date: 2024-01-20
Purpose/Change: Script interactif avec vérification de l'existence du process de MS Teams (ancien et nouveau)
.EXAMPLE
Clear-MSTeamsCache.ps1
#>
#---------------------------------------------------------[Initialisations]----------------------------------------
#TODO: ajout CmdLet pour prise arguments
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,Position = 0)]
[string]$SamAccountName
)
$MSTeamsNewProcessName = 'ms-teams'
$MSTeamsClassicProcessName = 'Microsoft Teams'
$CompanyName = "Microsoft Corporation"
$BasePath = "C:\Users\"
$AppDataMSTeamsRoamingFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Roaming\Teams"
$AppDataMSTeamsRoamingSubFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Roaming\Microsoft\Teams"
$AppDataAADPackageFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Packages\Microsoft.AAD.BrokerPlugin_cw5n1h2txyewy"
$AppDataOneAuthFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Microsoft\OneAuth"
$AppDataIdentityCacheFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Microsoft\IdentityCache"
$FoldersToDelete = @($AppDataMSTeamsRoamingFolder,$AppDataMSTeamsRoamingSubFolder,$AppDataAADPackageFolder,$AppDataOneAuthFolder,$AppDataIdentityCacheFolder)
#---------------------------------------------------------[Fonctions]----------------------------------------
# Fermeture forcée du process MS Teams (Classic ou New)
function Stop-MSTeams {
$MSTeamsProcessList = Get-Process | Where-Object {(($_.ProcessName -eq "$MSTeamsClassicProcessName") -or ($_.ProcessName -eq "$MSTeamsNewProcessName"))} | Select-Object -ExpandProperty ProcessName
if ($MSTeamsProcessList) {
foreach ($MSTeamsProcessName in $MSTeamsProcessList) {
Stop-Process -Force -Name $MSTeamsProcessName
}
}
}
# Affichage construction des chemins des répertoires de cache
function Write-CacheFolder {
foreach ($IndividualFolder in $FoldersToDelete) {
Write-Host Remove-Item -Recurse -Force -Path $IndividualFolder
}
}
2023-12-22 20:00:00 +01:00
# Demande confirmation de la suppression
function Read-ExecutionUserConfirm {
$Confirmation = Read-Host "Confirmer (y) ou (o) ?"
if (($Confirmation -eq 'y') -or ($Confirmation -eq 'o')) {
Write-Host "Lancement effacement cache"
} else {
$ScriptExitMessage = "Sortie du script"
Write-Host $ScriptExitMessage
exit
}
}
2023-12-22 20:00:00 +01:00
# Lancement suppression des répertoires
function Remove-CacheFolder {
foreach ($IndividualFolder in $FoldersToDelete) {
Remove-Item -Recurse -Force -Path $IndividualFolder
}
2023-12-22 20:00:00 +01:00
}
#---------------------------------------------------------[Exécution]----------------------------------------
Stop-MSTeams
Write-CacheFolder
Read-ExecutionUserConfirm
Remove-CacheFolder