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 .SYNOPSIS
Nettoyage du cache de Microsoft Teams et des Credentials compte Microsoft pour le module Microsoft.AAD.BrokerPlugin 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 .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> .PARAMETER SamAccountName
-SamAccountName Nom du répertoire du profil utilisateur sur lequel agir
.INPUTS .INPUTS
Aucun Aucun
.OUTPUTS .OUTPUTS
Aucun Aucun
.NOTES .NOTES
Version: 0.2.1 Version: 0.2.2
Author: Simon404 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) Purpose/Change: Script interactif avec vérification de l'existence du process de MS Teams (ancien et nouveau)
.EXAMPLE .EXAMPLE
Clear-MSTeamsCache.ps1 Clear-MSTeamsCache.ps1 -SamAccountName userprofile
.LINK
https://forge.chapril/Simon404
#> #>
#---------------------------------------------------------[Initialisations]---------------------------------------- #---------------------------------------------------------[Initialisations]----------------------------------------
#TODO(Simon404): passage en Module
[CmdletBinding()] [CmdletBinding()]
Param ( Param (
[Parameter(Mandatory=$true,Position = 0)] [Parameter(Mandatory=$true, ValueFromPipeline = $true, Position = 0)]
[string]$SamAccountName [string]$SamAccountName
) )
$MSTeamsNewProcessName = 'ms-teams' function Clear-MSTeamCache {
$MSTeamsClassicProcessName = 'Teams'
$BasePath = $env:USERPROFILE | SplitPath -Parent
$AppDataMSTeamsRoamingFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Roaming\Teams" $global:MSTeamsNewProcessName = 'ms-teams'
$AppDataMSTeamsRoamingSubFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Roaming\Microsoft\Teams" $global:MSTeamsClassicProcessName = 'Teams'
$AppDataAADPackageFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Packages\Microsoft.AAD.BrokerPlugin_cw5n1h2txyewy" $global:CompanyName = "Microsoft Corporation"
$AppDataOneAuthFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Microsoft\OneAuth"
$AppDataIdentityCacheFolder = Join-Path -Path $BasePath -ChildPath "$SamAccountName\AppData\Local\Microsoft\IdentityCache" $global:BasePath = $env:USERPROFILE | Split-Path -Parent
$FoldersToDelete = @($AppDataMSTeamsRoamingFolder,$AppDataMSTeamsRoamingSubFolder,$AppDataAADPackageFolder,$AppDataOneAuthFolder,$AppDataIdentityCacheFolder) $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]---------------------------------------- #---------------------------------------------------------[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) # Fermeture forcée du process MS Teams (Classic ou New)
function Stop-MSTeams { 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) { if ($MSTeamsProcessList) {
foreach ($MSTeamsProcessName in $MSTeamsProcessList) { foreach ($MSTeamsProcessName in $MSTeamsProcessList) {
Write-Host -Object "Fermeture de $MSTeamsProcessName" 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 # Lancement suppression des répertoires
function Remove-CacheFolder { function Remove-CacheFolder {
foreach ($IndividualFolder in $FoldersToDelete) { foreach ($IndividualFolder in $FoldersToDelete) {
@ -88,7 +119,11 @@ function Remove-CacheFolder {
#---------------------------------------------------------[Exécution]---------------------------------------- #---------------------------------------------------------[Exécution]----------------------------------------
Clear-MSTeamCache
Test-OperatingSystem
Stop-MSTeams Stop-MSTeams
Write-CacheFolder Write-CacheFolder
Read-ExecutionUserConfirm Select-ExecutionUserConfirm
Remove-CacheFolder Remove-CacheFolder
#TODO(Simon404): vérifier fonctions à exporter pour module