UblionLog.psm1
$LogManagement = @{} Function Log { param( [Parameter(Mandatory=$true)][String]$msg ) $logFile = $LogManagement.LogFolder $data = (Get-date).ToString() + " " + $msg Add-Content $logFile $data } function Set-LogConfiguration { param ( [String] $LogFolder, [int] $LogHours, [int] $LogRetention) if ($LogFolder) { Set-ConfigurationValue -name LogFolder -value $LogFolder } else { $folder = Get-LeftConnectConfigurationFolder Set-ConfigurationValue -name LogFolder -value (Join-Path $folder "logfile.txt") } if ($LogHours) { Set-ConfigurationValue -name LogHours -value $LogHours } else { Set-ConfigurationValue -name LogHours -value 24 } if ($LogRetention) { Set-ConfigurationValue -name LogRetention -value $LogRetention } else { Set-ConfigurationValue -name LogRetention -value 7 } Load-LogConfiguration } function Load-LogConfiguration { $LogManagement.Hours = Get-ConfigurationValue -name LogHours $LogManagement.LogFolder = Get-ConfigurationValue -name LogFolder $LogManagement.LogRetention = Get-ConfigurationValue -name LogRetention } function Log-Management { if ($LogManagement) { Load-LogConfiguration } $logFile = $LogManagement.LogFolder $lastWrite = (get-item $logFile).LastWriteTime $timespan = new-timespan -hours ($LogManagement.Hours) if (((get-date) - $lastWrite) -gt $timespan) { # older Move-LogFile } else { # newer } if ((Get-ChildItem ($logFile+"*") | Measure-Object).Count -gt $LogManagement.LogRetention) { $files = Get-ChildItem ($logFile+"*") $file = ($files.Name | Where-Object {$_.Contains("_")} |Sort-Object {$_})[0] $fileToDelete = Get-ChildItem ($logFile+"_"+$file.Split("_")[1]) Remove-Item $fileToDelete.FullName } } function Move-LogFile { if ($LogManagement) { $extension = [int](Get-Date -UFormat %s -Millisecond 0) $logFile = $LogManagement.LogFolder Move-Item $logFile ($LogManagement.LogFolder+"_"+$extension) } } function Get-LeftConnectLog { param( [switch]$watching ) if ($LogManagement -ne $null){ $logFile = $LogManagement.LogFolder if ($watching) { Get-Content $logFile -Wait } else { get-content $logFile } } } Export-ModuleMember -function Log Export-ModuleMember -function Set-LogConfiguration Export-ModuleMember -function Load-LogConfiguration Export-ModuleMember -function Log-Management Export-ModuleMember -function Move-LogFile Export-ModuleMember -function Get-LeftConnectLog |