refactor: CmdletBinding, Stop-MSTeams, help

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.
This commit is contained in:
Simon404 2024-02-10 20:00:00 +01:00
parent 78691ec810
commit c5f839c176
1 changed files with 87 additions and 24 deletions

View File

@ -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 <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
}
}
# 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