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 .REQUIREMENT Variables PathTest, FileTest, Counter1, LogLine .REQUIREMENT Variables preSet LogFile, LogPath, LogPaths, ScriptName, ScriptPath .REQUIREMENT Functions .VERSION Number: 2.0.0.0 / Date: 21.11.2016 .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." #> [cmdletbinding()] Param ( [Parameter(Position=0,Mandatory=$True,HelpMessage='Give the String you want to be written into the Logfile.')] [AllowEmptyString()] [String]$LogLine ) #end param #Clear Error Variable $error.clear() # This if / else block is only for determine the LogPath IF (!$LogPath) { Write-Host "" Write-Host "DEBUG Info: 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: Testing LogPath: $LogPath" $PathTest = Test-Path -PathType Container "$LogPath" #Check if Path already exists IF ($PathTest -eq $True) { Write-Host "DEBUG Info: LogPath seems already to exists: $LogPath" } #end if ELSE { #If Path doesnt exist, try to create it! Try { Write-Host "DEBUG Info: Trying to create LogPath: $LogPath" New-Item -ItemType Directory -Path "$LogPath" -Force -ErrorAction Stop | Out-Null Write-Host "DEBUG Info: Trying seems to be successful!" } #end try Catch { Write-Host "DEBUG Info: 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: 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 Test seems to be successful." Set-Variable -Name LogPath -Value $LogPath -Scope Global -Force } #end try Catch { Write-Host "DEBUG Info: Cannot write into LogPath or access denied!" Write-Host $Error } #end catch } #end if ELSE { Write-Host "DEBUG Info: 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: LogPath was already been set!" #Write-Host "DEBUG Info: 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 } #end try Catch { Write-Host "DEBUG Info: Cannot write to LogFile!" Write-Host "DEBUG Info: Exiting, because of this Issue." Write-Host $Error EXIT } #end catch } #end if ELSE { Write-Host "DEBUG Info: Cannot write to LogFile!" Write-Host "DEBUG Info: Exiting, because of this Issue." Write-Host $Error EXIT } #end else } #end function