scripts/skel.ps1

<#
 
    .SYNOPSIS
    Skeleton script for my tiny powershell framework
 
    .DESCRIPTION
    Tiny powershell framework.
 
    To ease programming here is debugging levels :
        -v : display VERBOSE level messages
        -d : display DEBUG level messages
        -dev : display DEVEL level messages (including DEBUG ones)
        -trace : display the line of script currently executed as well as DEVEL and DEBUG level messages
        -ask : ask user before each execution
        -q : silence all displays
 
    .PARAMETER h
    display help screen. Use Get-Help instead.
 
    .PARAMETER v
    enable verbose mode
 
    .PARAMETER d
    enable debug mode
 
    .PARAMETER dev
    enable devel mode
 
    .PARAMETER trace
    .enable trace mode. With this mode on you can trace entering and leaving every single function that use the Write-EnterFunction and Write-LeaveFunction calls.
    Very useful while developing a new script.
 
    .PARAMETER ask
    ask for each action
 
    .PARAMETER quiet
    quiet output completely
 
    .PARAMETER log
    log calls to e*() functions into specified logfile.
    If used in conjunction with -trace, it will use PowerShell Start-Transcript to log everything, including output of commands.
    Useful if you can't see the output of script for whatever reason. In this case, Write-ToLog() is deactivated.
 
    .NOTES
    Author: Charles-Antoine Degennes <cadegenn@gmail.com>
 
    .LINK
        https://github.com/cadegenn/pwsh_fw
#>


[CmdletBinding()]Param(
    [switch]$h,
    [switch]$v,
    [switch]$d,
    [switch]$dev,
    [switch]$trace,
    [switch]$ask,
    [switch]$quiet,
    [ValidateScript({
        Test-Path -Path $_ -PathType container
    })][string]$api,
    [string]$log,
    [ValidateScript({
        Test-Path -Path $_ -PathType leaf
    })][string]$configFile,
    [switch]$Force
)

$Global:BASENAME = Split-Path -Leaf $MyInvocation.MyCommand.Definition
$Global:VERBOSE = $v
$Global:DEBUG = $d
$Global:DEVEL = $dev
$Global:TRACE = $trace
$Global:ASK = $ask
$Global:QUIET = $quiet
$Global:LOG = $log
$rc = Get-Module -Name PwSh.Fw.Core
if (!$rc) {
    $rc = Import-Module PwSh.Fw.Core -ErrorAction Stop -DisableNameChecking
}
$modules = @()

if ($h) {
    Get-Help $MyInvocation.MyCommand.Definition
    Exit
}

# keep the order as-is please !
$oldDebugPreference = $DebugPreference
$oldVerbosePreference = $VerbosePreference
$oldInformationPreference = $InformationPreference
if ($ASK)   { Set-PSDebug -Step }
if ($TRACE) {
    # Set-PSDebug -Trace 1
    $Global:DEVEL = $true
}
if ($DEVEL) {
    $Global:DEBUG = $true;
}
if ($DEBUG) {
    $DebugPreference="Continue"
    $Global:VERBOSE = $true
}
if ($VERBOSE) {
    $VerbosePreference="Continue"
}
if ($QUIET) {
    $Global:DEVEL = $false
    $Global:DEBUG = $false;
    $Global:VERBOSE = $false
}

if ($log) {
    if ($TRACE) {
        Start-Transcript -Path $log
    # } else {
    # # add -Append:$false to overwrite logfile
    # # Write-ToLogFile -Message "Initialize log" -Append:$false
    # Write-ToLogFile -Message "Initialize log"
    }
    $modules += "PwSh.Fw.Log"
}

# write-output "Language mode :"
# $ExecutionContext.SessionState.LanguageMode

#
# Load Everything
#
everbose("Loading modules")
# $modules += "PsIni"
# $modules += "PwSh.ConfigFile"
# $modules += "Microsoft.PowerShell.Archive"
# USER MODULES HERE
$ERRORFOUND = $false
ForEach ($m in $modules) {
    $rc = Load-Module -Name $m -Force:$Force
    if ($rc -eq $false) { $ERRORFOUND = $true }
}
if ($ERRORFOUND) { efatal("At least one module could not be loaded.") }

#############################
## YOUR SCRIPT BEGINS HERE ##
#############################

<#
 
  ###### ######## ### ######## ########
 ## ## ## ## ## ## ## ##
 ## ## ## ## ## ## ##
  ###### ## ## ## ######## ##
       ## ## ######### ## ## ##
 ## ## ## ## ## ## ## ##
  ###### ## ## ## ## ## ##
 
#>


function Write-Test {
    begin {
        Write-EnterFunction
    }
    process {
        einfo "Where are we ?"
        edebug "where does this piece of code comes frome ?"
        edebug "to know it, run $BASENAME with -dev parameter..."
    }
    end {
        Write-LeaveFunction
    }
}
etitle ("$Global:BASENAME")
edevel "VERBOSE = $Global:VERBOSE"
edevel "DEBUG = $Global:DEBUG"
edevel "DEVEL = $Global:DEVEL"
edevel "QUIET = $Global:QUIET"
einfo "This is an info message"
einfo "Try launching this script with various parameters like -v -d -dev"
everbose "This is a verbose message"
edebug "This is a debug message"
edevel "This is a message for developer"
ebegin "Start an action"
eend $?
Write-Test
eenter "Enter a loop"
1..5 | ForEach-Object { everbose $_ }
eleave "Leaving the loop"
einfo "you can execute commands (try it with -d and -dev)"
$rc = eexec "hostname"

<#
 
 ######## ## ## ########
 ## ### ## ## ##
 ## #### ## ## ##
 ###### ## ## ## ## ##
 ## ## #### ## ##
 ## ## ### ## ##
 ######## ## ## ########
 
#>


#############################
## YOUR SCRIPT ENDS HERE ##
#############################

if ($log) {
    if ($TRACE) {
        Stop-Transcript
    } else {
        Write-ToLogFile -Message "------------------------------------------"
    }
}

# reinit values
$Global:DebugPreference = $oldDebugPreference
$Global:VerbosePreference = $oldVerbosePreference
$Global:InformationPreference = $oldInformationPreference
Set-PSDebug -Off