8
0
Skriptentwickung/current/Modules/Write-LogFile.psm1
2024-01-24 16:42:38 +01:00

232 lines
7.2 KiB
PowerShell

Function Write-LogFile {
<#
.SYNOPSIS
Function will write a given String to a Logfile.
.DESCRIPTION
Just like the cmdlet Write-Host, this Function will display Output on the Console.
Parallel Output will be written into a designated LogFile.
It is recommended to init the LogPath Variable with $Null and the LogPaths Variable with a possible Path,
which has to be checked for accessible.
This is important, because if given Path was inaccessible (-> Access Test failed), Function will try some other Paths for accessible.
These are: The ScriptPath (\Logs) from, which triggerd this Function and $env:temp\Digital Data\$ScriptName\Logs.
After a successful Access Test, Function will set the Variable LogPath, with the first accessible Path tested.
Function will not give any Return Values, because if it is impossible to write a LogFile, Function will force the whole Script to exit.
.REQUIREMENT General
PowerShell V2
.REQUIREMENT Assembly
<NONE>
.REQUIREMENT Variables
PathTest, FileTest, LogPaths, Counter1, LogLine
.REQUIREMENT Variables preSet
LogFile, LogPath, ScriptName, ScriptPath
.REQUIREMENT Functions
<NONE>
.VERSION
Number: 2.1.0.1 / Date: 23.09.2017
.PARAMETER LogLine
Give the String you want to be written into the Logfile.
.EXAMPLE
Write-LogFile -LogLine "Write this in my Log, please."
.EXAMPLE
Write-LogFile -LogLine "Write this Variabel $Variable in my Log, please."
#>
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage='Give the String you want to be written into the Logfile.')]
[AllowEmptyString()]
[STRING]$LogLine,
[Parameter(Position=1,Mandatory=$False,HelpMessage='Give the String you want to be written into the Logfile.')]
[ValidateNotNullOrEmpty()]
[STRING]$LogFile=(Get-Variable -Name LogFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue),
[Parameter(Position=2,Mandatory=$False,HelpMessage='Give the String you want to be written into the Logfile.')]
[ValidateNotNullOrEmpty()]
[STRING]$LogPath=(Get-Variable -Name LogPath -Scope Global -ValueOnly -ErrorAction SilentlyContinue),
[Parameter(Position=3,Mandatory=$False,HelpMessage='Give the String you want to be written into the Logfile.')]
[ValidateNotNullOrEmpty()]
[STRING]$ScriptName=(Get-Variable -Name ScriptName -Scope Global -ValueOnly -ErrorAction SilentlyContinue),
[Parameter(Position=4,Mandatory=$False,HelpMessage='Give the String you want to be written into the Logfile.')]
[ValidateNotNullOrEmpty()]
[STRING]$ScriptPath=(Get-Variable -Name ScriptPath -Scope Global -ValueOnly -ErrorAction SilentlyContinue)
) #end param
#Clear Error Variable
$error.clear()
IF (!$LogFile) {
$LogFile = ((($MyInvocation.MyCommand.Name) -split "\.")[0].ToString())+".log"
Write-Host "DEBUG Info - Write-LogFile: FailSafe Setting for LogFile, was set to: $LogFile"
} #end if
IF (!$LogPath) {
$LogPath = $PSScriptRoot+"\Logs"
Write-Host "DEBUG Info - Write-LogFile: FailSafe Setting for LogPath, was set to: $LogPath"
} #end if
IF (!$ScriptName) {
$ScriptName = ((($MyInvocation.MyCommand.Name) -split "\.")[0].ToString())
Write-Host "DEBUG Info - Write-LogFile: FailSafe Setting for ScriptName, was set to: $ScriptName"
} #end if
IF (!$ScriptPath) {
$ScriptPath = $PSScriptRoot
Write-Host "DEBUG Info - Write-LogFile: FailSafe Setting for ScriptPath, was set to: $ScriptPath"
} #end if
# This if / else block is only for determine the LogPath
IF ((!$LogPath) -or ($LogPath -eq $PSScriptRoot+"\Logs")) {
Write-Host ""
Write-Host "DEBUG Info - Write-LogFile: LogPath is currently not set!"
[Array]$LogPaths = $LogPaths
[Array]$LogPaths = ($LogPaths += "$ScriptPath\Logs","$env:temp\Digital Data\$ScriptName\Logs")
[int]$Counter1 = 0
DO {
#Determine the current Array object
[String]$LogPath = [Array]$($LogPaths[$Counter1])
Write-Host ""
Write-Host "DEBUG Info - Write-LogFile: Testing LogPath: $LogPath"
$PathTest = Test-Path -PathType Container "$LogPath"
#Check if Path already exists
IF ($PathTest -eq $True) {
Write-Host "DEBUG Info - Write-LogFile: LogPath seems already to exists: $LogPath"
} #end if
ELSE {
#If Path doesnt exist, try to create it!
Try {
Write-Host "DEBUG Info - Write-LogFile: Trying to create LogPath: $LogPath"
New-Item -ItemType Directory -Path "$LogPath" -Force -ErrorAction Stop | Out-Null
Write-Host "DEBUG Info - Write-LogFile: Trying seems to be successful!"
} #end try
Catch {
Write-Host "DEBUG Info - Write-LogFile: Cannot create LogPath or access denied!"
Write-Host $Error
} #end catch
} #end else
#Check again if path exists
$PathTest = Test-Path -PathType Container "$LogPath"
IF ($PathTest -eq $True) {
Try {
Write-Host "DEBUG Info - Write-LogFile: Trying to write a TestFile into the LogPath."
New-Item -ItemType File -Path "$LogPath\AccessTest.txt" -Force -ErrorAction Stop | Out-Null
Add-content "$LogPath\AccessTest.txt" -Value "This is a Test!" -Force -ErrorAction Stop | Out-Null
$FileTest = Test-Path -Path "$LogPath\AccessTest.txt" -PathType Leaf -ErrorAction Stop
Remove-Item -Path "$LogPath\AccessTest.txt" -Force -ErrorAction Stop | Out-Null
Write-Host "DEBUG Info - Write-LogFile: Write Test seems to be successful."
Set-Variable -Name LogPath -Value $LogPath -Scope Global -Force
} #end try
Catch {
Write-Host "DEBUG Info - Write-LogFile: Cannot write into LogPath or access denied!"
Write-Host $Error
} #end catch
} #end if
ELSE {
Write-Host "DEBUG Info - Write-LogFile: Cannot create LogPath or access denied!"
Write-Host $Error
} #end else
[int]$Counter1++ | Out-Null
} #end do
UNTIL ((($PathTest -eq $True) -and ($FileTest -eq $True)) -or ($Counter1 -eq $($LogPaths.count)) )
} #end if
ELSEIF ($LogPath) {
#Write-Host "DEBUG Info - Write-LogFile: LogPath was already been set!"
#Write-Host "DEBUG Info - Write-LogFile: LogPath is $LogPath"
} #end elseif
ELSE {
Write-Host "Unexpected Error!"
Write-Host $Error
Return $False
} #end else
IF ($LogPath) {
#After LogPath determination - try to log the string
Try {
Write-Host "$LogLine"
Add-content $LogPath\$LogFile -value "$(Get-Date -Format 'dd.MM.yyyy')-$(Get-Date -Format 'HH:mm:ss'): $LogLine" -ErrorAction Stop
#Return $True
} #end try
Catch {
Write-Host "DEBUG Info - Write-LogFile: Cannot write to LogFile!"
Write-Host "DEBUG Info - Write-LogFile: Exiting, because of this Issue."
Write-Host $Error
EXIT
} #end catch
} #end if
ELSE {
Write-Host "DEBUG Info - Write-LogFile: Cannot write to LogFile!"
Write-Host "DEBUG Info - Write-LogFile: Exiting, because of this Issue."
Write-Host $Error
EXIT
} #end else
} #end function