#Requires -Version 5.1<#.SYNOPSIS Description courte du script..DESCRIPTION Description détaillée du script et de son fonctionnement..PARAMETER Param1 Description du paramètre 1..PARAMETER Param2 Description du paramètre 2..EXAMPLE .\Script-Name.ps1 -Param1 "Value" Description de l'exemple..NOTES Author: ShellBook Version: 1.0 Date: 2024-01-01#>[CmdletBinding()]param([Parameter(Mandatory=$false)][string]$Param1="Default",[Parameter(Mandatory=$false)][switch]$Verbose)#region Configuration$ErrorActionPreference='Stop'Set-StrictMode-VersionLatest#endregion#region FunctionsfunctionWrite-Log{param([string]$Message,[ValidateSet('Info','Warning','Error')][string]$Level='Info')$timestamp=Get-Date-Format"yyyy-MM-dd HH:mm:ss"$colors=@{'Info'='Green''Warning'='Yellow''Error'='Red'}Write-Host"[$timestamp] [$Level] $Message"-ForegroundColor$colors[$Level]}#endregion#region Maintry{Write-Log"Script démarré"-LevelInfo# Code principal iciWrite-Log"Script terminé"-LevelInfo}catch{Write-Log"Erreur: $_"-LevelErrorexit1}#endregion
Bonnes Pratiques PowerShell
Paramètres
# Utiliser CmdletBinding pour les fonctionnalités avancées[CmdletBinding(SupportsShouldProcess)]param([Parameter(Mandatory,ValueFromPipeline)][ValidateNotNullOrEmpty()][string]$Path,[Parameter()][ValidateRange(1,100)][int]$Threshold=80)
Gestion d'erreurs
try{# Code à risque$result=Get-Item-Path$Path-ErrorActionStop}catch[System.IO.FileNotFoundException]{Write-Warning"Fichier non trouvé: $Path"}catch{Write-Error"Erreur inattendue: $_"throw}finally{# Nettoyage}
Output Formaté
# Créer des objets pour un output propre[PSCustomObject]@{Name=$item.NameSize="{0:N2} MB"-f($item.Length/1MB)Modified=$item.LastWriteTimeStatus="OK"}