Exercice récapitulatif — Partie 2
Synthèse : fonctions + if/switch + boucles + actions de masse + erreurs + logs.
Écrire un script “outil d’exploitation”
Produire un script unique, lisible et robuste, qui réalise un audit (services + chemins), génère un rapport et journalise les erreurs.
Énoncé
- Créer un script :
Part2-Recap.ps1 - Le script demande à l’utilisateur (Read-Host) :
- le chemin de
services.txt - le chemin de
paths.txt - un “mode” via un menu (switch) :
1=Audit,2=Audit+Création
- le chemin de
- Le script contient au moins 2 fonctions :
Get-ServiceAuditRow(retourne un objet custom)Get-PathAuditRow(retourne un objet custom)
- Le script boucle (foreach) sur :
- la liste des services (Get-Content)
- la liste des chemins (Get-Content)
- Le script utilise if (ex : service introuvable / chemin présent) et switch (menu).
- Le script gère les erreurs avec Try/Catch, écrit un log (dossier logs).
- Le script exporte un rapport CSV horodaté (dossier reports).
Critère “pro” : relançable, lisible, sorties exploitables (rapport + log), aucun crash non géré.
Squelette recommandé
# Part2-Recap.ps1
$base = "C:\tssr-powershell"
$logDir = Join-Path $base "logs"
$repDir = Join-Path $base "reports"
New-Item -ItemType Directory -Force -Path $logDir, $repDir | Out-Null
$ts = Get-Date -Format "yyyyMMdd_HHmmss"
$logFile = Join-Path $logDir "recap_errors_$ts.log"
$reportFile = Join-Path $repDir "recap_report_$ts.csv"
function Get-ServiceAuditRow {
param([string]$Name)
$svc = Get-Service -Name $Name -ErrorAction SilentlyContinue
if ($null -eq $svc) {
return [pscustomobject]@{Type="Service";Name=$Name;Status="NotFound";Action="None"}
}
return [pscustomobject]@{Type="Service";Name=$svc.Name;Status=$svc.Status;Action="Checked"}
}
function Get-PathAuditRow {
param([string]$Path, [string]$Mode)
if (Test-Path $Path) {
return [pscustomobject]@{Type="Path";Name=$Path;Status="Present";Action="Checked"}
}
if ($Mode -eq "2") {
New-Item -ItemType Directory -Force -Path $Path | Out-Null
return [pscustomobject]@{Type="Path";Name=$Path;Status="Created";Action="New-Item"}
}
return [pscustomobject]@{Type="Path";Name=$Path;Status="Absent";Action="None"}
}
try {
$servicesFile = Read-Host "Chemin services.txt"
$pathsFile = Read-Host "Chemin paths.txt"
$mode = Read-Host "Mode : 1=Audit, 2=Audit+Creation"
switch ($mode) {
"1" { Write-Output "Mode AUDIT" }
"2" { Write-Output "Mode AUDIT + CREATION" }
default { throw "Mode invalide : $mode" }
}
$rows = @()
foreach ($s in (Get-Content $servicesFile)) {
$rows += Get-ServiceAuditRow -Name $s
}
foreach ($p in (Get-Content $pathsFile)) {
$rows += Get-PathAuditRow -Path $p -Mode $mode
}
$rows | Export-Csv -NoTypeInformation -Encoding UTF8 -Path $reportFile
Write-Output "Rapport : $reportFile"
}
catch {
Add-Content -Path $logFile -Value "$(Get-Date) - ERREUR - $($_.Exception.Message)"
Write-Output "Erreur capturée. Log : $logFile"
}
Grille d’auto-évaluation
- Le script s’exécute sans modification après saisie (Read-Host)
- Le menu switch fonctionne (mode 1 / mode 2)
- Deux fonctions minimum, paramètres compris
- if présents (présence/absence)
- foreach pour traiter les listes
- Try/Catch + log horodaté
- Rapport CSV horodaté généré
- Code propre : sections + commentaires + variables explicites