From c5f839c1762f21b83a16fb830774218714f7524c Mon Sep 17 00:00:00 2001 From: Simon404 Date: Sat, 10 Feb 2024 20:00:00 +0100 Subject: [PATCH] refactor: CmdletBinding, Stop-MSTeams, help MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Réécriture arrêt des processus, ajout de paramètres en entrée et d'une aide interne Le nom de profil utilisateur peut être entré en argument. Stop-MSTeams recherche les processus de MS Teams classic et New. --- Clear-MSTeamsCache.ps1 | 111 ++++++++++++++++++++++++++++++++--------- 1 file changed, 87 insertions(+), 24 deletions(-) diff --git a/Clear-MSTeamsCache.ps1 b/Clear-MSTeamsCache.ps1 index 66271d6..ec84a8f 100755 --- a/Clear-MSTeamsCache.ps1 +++ b/Clear-MSTeamsCache.ps1 @@ -1,31 +1,94 @@ -# Clean Microsoft Teams Cache after stopping process +<# +.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 -$SamAccountName = Read-Host -Prompt "SamAccountName" -$BasePath = "C:\Users" -$FoldersToDelete = @("$BasePath\$SamAccountName\AppData\Roaming\Teams","$BasePath\$SamAccountName\AppData\Roaming\Microsoft\Teams","$BasePath\$SamAccountName\AppData\Local\Packages\Microsoft.AAD.BrokerPlugin_cw5n1h2txyewy","$BasePath\$SamAccountName\AppData\Local\Microsoft\OneAuth","$BasePath\$SamAccountName\AppData\Local\Microsoft\IdentityCache") +.DESCRIPTION + Suppression des fichiers de cache de MS Teams et des credentials du module Microsoft.AAD.BrokerPlugin -Function Stop-MSTeams { - $MSTeamsProcessList = Get-Process -Name "Teams" -ErrorAction SilentlyContinue | Where-Object {($_.Description -eq "Microsoft Teams") -and ($_.Company -eq "Microsoft Corporation")} - if ($MSTeamsProcessList) { - # try gracefully first - $MSTeamsProcessList.CloseMainWindow() - # kill after five seconds - Sleep 5 - if (-not $MSTeamsProcessList.HasExited) { - $MSTeamsProcessList | - Stop-Process -Force +.PARAMETER + -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 } - } - # Source : https://stackoverflow.com/a/28482050 - -} - -Function Remove-MSTeamsCacheDirectory { - foreach ($IndividualFolder in $FoldersToDelete) { - Write-Host Remove-Item -Recurse -Force -Path $IndividualFolder } } -Stop-MSTeams -Remove-MSTeamsCacheDirectory +# Affichage construction des chemins des répertoires de cache +function Write-CacheFolder { + foreach ($IndividualFolder in $FoldersToDelete) { + Write-Host Remove-Item -Recurse -Force -Path $IndividualFolder + } +} +# 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 + } +} + +# Lancement suppression des répertoires +function Remove-CacheFolder { + foreach ($IndividualFolder in $FoldersToDelete) { + Remove-Item -Recurse -Force -Path $IndividualFolder + } +} + +#---------------------------------------------------------[Exécution]---------------------------------------- + +Stop-MSTeams +Write-CacheFolder +Read-ExecutionUserConfirm +Remove-CacheFolder