Skip to content

Instantly share code, notes, and snippets.

@techdecline
Created September 26, 2018 10:47
Show Gist options
  • Save techdecline/684261afe59e6e996bf1c09ddb2edd9e to your computer and use it in GitHub Desktop.
Save techdecline/684261afe59e6e996bf1c09ddb2edd9e to your computer and use it in GitHub Desktop.
Function Write-Log
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$LogFile,
[Parameter(Mandatory=$False)]
[switch]$HiddenLogFile,
[Parameter(Mandatory=$False)]
[switch]$HiddenLogPath,
[Parameter(Mandatory=$False)]
[switch]$ClearLog,
[Parameter(Mandatory=$False)]
[ValidateRange(0,2)]
[Int]$CritLevel,
[Parameter(Mandatory=$False)]
[switch]$Start,
[Parameter(Mandatory=$False)]
[switch]$Stop,
[Parameter(Mandatory=$False)]
[switch]$NewLine,
[Parameter(Mandatory=$False)]
[String]$LogMessage
)
$Date = Get-Date -Format yyyy-MM-dd
$Time = Get-Date -Format HH:mm
$LogPath = Split-Path -Path $LogFile
#Ordner ueberpruefen und ggf. anlegen
if (!(Test-Path $LogPath))
{
if ($HiddenLogPath -eq $True)
{
New-Item $LogPath -type directory | %{$_.Attributes = "hidden"}
}
else
{
New-Item $LogPath -type directory | Out-Null
}
}
#Logfile ueberpruefen und ggf. anlegen
if (!(Test-Path $LogFile))
{
if ($HiddenLogFile -eq $True)
{
New-Item -Path $LogPath -Name (Split-Path -Path $LogFile -Leaf) -ItemType File | %{$_.Attributes = "hidden"}
}
else
{
New-Item -Path $LogPath -Name (Split-Path -Path $LogFile -Leaf) -ItemType File | Out-Null
}
}
#Start Log
If ($Start -eq $True)
{
If ($ClearLog -eq $True)
{
Clear-Content -Path $LogFile
}
Add-Content -Path $LogFile -Value "==================================================================================================="
Add-Content -Path $LogFile -Value "Started processing at [$([DateTime]::Now)]"
Add-Content -Path $LogFile -Value "==================================================================================================="
Add-Content -Path $LogFile -Value ""
}
# Set critlevel prefix
switch ($CritLevel)
{
0 {$Prefix = "[$([DateTime]::Now)] Info: "}
1 {$Prefix = "[$([DateTime]::Now)] Warning: "}
2 {$Prefix = "[$([DateTime]::Now)] Error: "}
default {$Prefix = "[$([DateTime]::Now)] Info: "}
}
#LogMessage
If ($LogMessage -ne "")
{
If ($env:USERNAME -eq "rollem") {Write-Host ($Prefix + $LogMessage)}
Add-Content -Path $LogFile -Value ($Prefix + $LogMessage)
}
#NewLine
If ($NewLine -eq $True)
{
Add-Content -Path $LogFile -Value ""
}
#Stop Log
If ($Stop -eq $True)
{
Add-Content -Path $LogFile -Value ""
Add-Content -Path $LogFile -Value "==================================================================================================="
Add-Content -Path $LogFile -Value "Finished processing at [$([DateTime]::Now)]"
Add-Content -Path $LogFile -Value "==================================================================================================="
Add-Content -Path $LogFile -Value ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment