1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
@'
$Script:LogFileName ="C:\temp\LogFileName.txt"
#$CurrentScript = "$PSCommandPath".Substring($PSScriptRoot.Length+1)
#$Script:CS = $CurrentScript.PadRight(20)
#$CST = $CS + " " + (get-date -Format G) + " " + $MSGLOG01
Function Add-Log {
[cmdletBinding()]
Param(
[PARAMETER(Mandatory=$true, HelpMessage='My First Argument')]
[string] $MSGLOG01
)
$CallerScript=$PSCmdlet.SessionState.PSVariable.GetValue('PSCommandPath')
Write-Debug $CallerScript
$CallerScriptRoot=$PSCmdlet.SessionState.PSVariable.GetValue('PSScriptRoot')
Write-Debug $CallerScriptRoot
$Caller=$CallerScript.Substring($CallerScriptRoot.Length+1)
$Header = (get-date -Format G) + ' ' + $($Caller.PadRight(20)) +' '
$CST = $Header + $MSGLOG01
Write-host $MSGLOG01 -ForegroundColor black -BackgroundColor darkgray
Add-Content $LogFileName $CST
$CallStack="Call Stack`r`n$(Get-PSCallStack|Out-String)"
Write-Debug $CallStack
Add-Content $LogFileName $CallStack
Add-Content $LogFileName 'La suite'
}
Function Get-LogFileName{
param()
return $Script:LogFileName
}
Export-ModuleMember -Function 'Add-Log','Get-LogFileName'
'@ > c:\temp\Log.psm1
@'
Import-Module C:\temp\Log.psm1 -force
"Fait qq chose"
Add-Log 'Message 1'
'@ > c:\temp\CallLog.ps1
cd c:\temp
.\CallLog.ps1
$F=get-LogFileName
type $F |
Partager