refactor: scopes variables, fonctions renommées

Ajout de scope aux variables globales et suivi préconisation PowerShell dans dans nommage des fonctions en suivant référence Get-Verb
This commit is contained in:
Simon404 2024-02-22 00:14:24 +01:00
parent 23e4bb00f3
commit 65fc0eff85
1 changed files with 71 additions and 36 deletions

View File

@ -1,57 +1,100 @@
<#
.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
Nettoyage du cache de Microsoft Teams et des Credentials compte Microsoft pour le module Microsoft.AAD.BrokerPlugin
.DESCRIPTION
Suppression des fichiers de cache de MS Teams et des credentials du module Microsoft.AAD.BrokerPlugin
Suppression des fichiers de cache de MS Teams et des credentials du 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
.PARAMETER <Parameter_Name>
-SamAccountName
.PARAMETER SamAccountName
Nom du répertoire du profil utilisateur sur lequel agir
.INPUTS
Aucun
Aucun
.OUTPUTS
Aucun
Aucun
.NOTES
Version: 0.2.1
Version: 0.2.2
Author: Simon404
Creation Date: 2024-02-12
Creation Date: 2024-02-21
Purpose/Change: Script interactif avec vérification de l'existence du process de MS Teams (ancien et nouveau)
.EXAMPLE
Clear-MSTeamsCache.ps1
Clear-MSTeamsCache.ps1 -SamAccountName userprofile
.LINK
https://forge.chapril/Simon404
#>
#---------------------------------------------------------[Initialisations]----------------------------------------
#TODO(Simon404): passage en Module
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,Position = 0)]
[Parameter(Mandatory=$true, ValueFromPipeline = $true, Position = 0)]
[string]$SamAccountName
)
$MSTeamsNewProcessName = 'ms-teams'
$MSTeamsClassicProcessName = 'Teams'
function Clear-MSTeamCache {
$BasePath = $env:USERPROFILE | SplitPath -Parent
$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)
$global:MSTeamsNewProcessName = 'ms-teams'
$global:MSTeamsClassicProcessName = 'Teams'
$global:CompanyName = "Microsoft Corporation"
$global:BasePath = $env:USERPROFILE | Split-Path -Parent
$global:AppDataMSTeamsRoamingFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Roaming\Teams"
$global:AppDataMSTeamsRoamingSubFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Roaming\Microsoft\Teams"
$global:AppDataAADPackageFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Packages\Microsoft.AAD.BrokerPlugin_cw5n1h2txyewy"
$global:AppDataOneAuthFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Microsoft\OneAuth"
$global:AppDataIdentityCacheFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Microsoft\IdentityCache"
$global:FoldersToDelete = @($AppDataMSTeamsRoamingFolder,$AppDataMSTeamsRoamingSubFolder,$AppDataAADPackageFolder,$AppDataOneAuthFolder,$AppDataIdentityCacheFolder)
}
#---------------------------------------------------------[Fonctions]----------------------------------------
# Vérification de l'exécution sous Windows
function Test-OperatingSystem {
$OSIdentifier = Get-ChildItem -Path Env: |
Where-Object { $_.Name -eq "OS" }
if ($OSIdentifier.Value-eq "Windows_NT") {
Write-Host -Object "Lancement dans environnement Windows OK"
} else {
Write-Host -Object "Système d'exploitation non supporté"
}
}
# Demande confirmation de la suppression
#TODO(Simon404): passer le choix de l'utilisateur en paramètre de fonction
function Select-ExecutionUserConfirm {
$ConfirmationMessage = "Confirmer (o) ou (y) ? | Quitter (q)"
$StartFunctionMessage = "Lancement de l'action"
$ExitScriptMessage = "Sortie du script"
$SkipFunctionMessage = "Poursuite de l'execution du script à l'étape suivante"
$Confirmation = Read-Host -Prompt $ConfirmationMessage
if (($Confirmation -eq 'o') -or ($Confirmation -eq 'y')) {
Write-Host $StartFunctionMessage
} elseif ($Confirmation -eq 'q') {
Write-Host $ScriptExitMessage
exit
} else {
Select-ExecutionUserConfirm
}
}
# 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") -and ($_.Company -eq "Microsoft Corporation"))} | Select-Object -ExpandProperty ProcessName
Write-Host -Object "Action : arrêt de MS Teams"
Select-ExecutionUserConfirm
$MSTeamsProcessList = Get-Process | Where-Object {(($_.ProcessName -eq "$MSTeamsClassicProcessName") -or ($_.ProcessName -eq "$MSTeamsNewProcessName") -and ($_.Company -eq "$CompanyName"))} | Select-Object -ExpandProperty ProcessName
if ($MSTeamsProcessList) {
foreach ($MSTeamsProcessName in $MSTeamsProcessList) {
Write-Host -Object "Fermeture de $MSTeamsProcessName"
@ -67,18 +110,6 @@ function Write-CacheFolder {
}
}
# 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) {
@ -88,7 +119,11 @@ function Remove-CacheFolder {
#---------------------------------------------------------[Exécution]----------------------------------------
Clear-MSTeamCache
Test-OperatingSystem
Stop-MSTeams
Write-CacheFolder
Read-ExecutionUserConfirm
Select-ExecutionUserConfirm
Remove-CacheFolder
#TODO(Simon404): vérifier fonctions à exporter pour module