LLI Wiki — My Digital Knowledge Base

PowerShell : modèle de script avec en-tête d'aide et journalisation

Squelette de script PowerShell (aide, initialisations, fonctions, journal) basé sur un gist public.

Modèle de script PowerShell avec en-tête d'aide, initialisations, déclarations, fonctions et exécution, basé sur un gist public (9to5IT/9620683). Il sert de point de départ pour des scripts d'administration qui écrivent un journal.

Structure du modèle

Section Contenu
En-tête d'aide Bloc de commentaire .SYNOPSIS, .DESCRIPTION, .PARAMETER, .INPUTS, .OUTPUTS, .NOTES, .EXAMPLE
Initialisations $ErrorActionPreference et chargement par « dot sourcing » d'une bibliothèque de fonctions de journalisation
Déclarations Version du script et chemin du fichier journal
Fonctions Squelette Begin / Process / End avec Try / Catch
Exécution Appels de démarrage et de fin de journal autour du code principal

Modèle

#requires -version 2
<#
.SYNOPSIS
  <Overview of script>
.DESCRIPTION
  <Brief description of script>
.PARAMETER <Parameter_Name>
    <Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
  <Inputs if any, otherwise state None>
.OUTPUTS
  <Outputs if any, otherwise state None>
.NOTES
  Version:        1.0
  Author:         <Name>
  Creation Date:  <Date>
  Purpose/Change: Initial script development
.EXAMPLE
  <Example goes here. Repeat this attribute for more than one example>
#>

#---------------------------------------------------------[Initialisations]--------------------------------------------------------

#Set Error Action to Silently Continue
$ErrorActionPreference = "SilentlyContinue"

#Dot Source required Function Libraries
. "C:\Scripts\Functions\Logging_Functions.ps1"

#----------------------------------------------------------[Declarations]----------------------------------------------------------

#Script Version
$sScriptVersion = "1.0"

#Log File Info
$sLogPath = "C:\Windows\Temp"
$sLogName = "<script_name>.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName

#-----------------------------------------------------------[Functions]------------------------------------------------------------

<#
Function <FunctionName>{
  Param()

  Begin{
    Log-Write -LogPath $sLogFile -LineValue "<description of what is going on>..."
  }

  Process{
    Try{
      <code goes here>
    }

    Catch{
      Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $True
      Break
    }
  }

  End{
    If($?){
      Log-Write -LogPath $sLogFile -LineValue "Completed Successfully."
      Log-Write -LogPath $sLogFile -LineValue " "
    }
  }
}
#>

#-----------------------------------------------------------[Execution]------------------------------------------------------------

#Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
#Script Execution goes here
#Log-Finish -LogPath $sLogFile

Remarques

Mastodon LLIMastodon SocialFosstodon