Review log creation

pull/683/head
Romain 2024-09-02 22:58:12 +02:00
parent d0334a8561
commit d987820bf8
2 changed files with 22 additions and 45 deletions

View File

@ -11,6 +11,7 @@ Get-ChildItem "$WorkingDir\functions" -File -Filter "*.ps1" -Depth 0 | ForEach-O
#Config console output encoding
$null = cmd /c ''
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$Script:ProgressPreference = 'SilentlyContinue'
#Log initialisation
$LogFile = "$WorkingDir\logs\updates.log"
@ -24,7 +25,7 @@ $Script:SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId
if ($IsSystem) {
#If log file doesn't exist, force create it
if (!(Test-Path -Path $LogFile)) {
New-Item -Path $LogFile -ItemType File -Force | Out-Null
Write-ToLog "New log file created"
}
# Check if Intune Management Extension Logs folder exists
if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue)) {

View File

@ -1,55 +1,28 @@
#Function to rotate the logs
function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
<#
.SYNOPSIS
Handle log rotation.
.DESCRIPTION
Invoke-LogRotation handles log rotation
.NOTES
Author: Øyvind Kallstad (Minimized and changed for WAU 12.01.2023 by Göran Axel Johannesson)
URL: https://www.powershellgallery.com/packages/Communary.Logger/1.1
Date: 21.11.2014
Version: 1.0
#>
try {
# get current size of log file
$currentSize = (Get-Item $LogFile).Length
# if MaxLogFiles is 1 just keep the original one and let it grow
if (-not($MaxLogFiles -eq 1)) {
# get log name
$logFileName = Split-Path $LogFile -Leaf
$logFilePath = Split-Path $LogFile
$logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName)
$logFileNameExtension = [System.IO.Path]::GetExtension($logFileName)
try {
# get current size of log file
$currentSize = (Get-Item $LogFile).Length
# get log name
$logFileName = Split-Path $LogFile -Leaf
$logFilePath = Split-Path $LogFile
$logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName)
$logFileNameExtension = [System.IO.Path]::GetExtension($logFileName)
# if MaxLogFiles is 1 just keep the original one and let it grow
if (-not($MaxLogFiles -eq 1)) {
if ($currentSize -ge $MaxLogSize) {
# construct name of archived log file
$newLogFileName = $logFileNameWithoutExtension + (Get-Date -Format 'yyyyMMddHHmmss').ToString() + $logFileNameExtension
Rename-Item -Path $LogFile -NewName $newLogFileName -Force -Confirm:$false
# copy old log file to new using the archived name constructed above
Copy-Item -Path $LogFile -Destination (Join-Path (Split-Path $LogFile) $newLogFileName)
# Create a new log file
try {
Remove-Item -Path $LogFile -Force
New-Item -ItemType File -Path $LogFile -Force
#Set ACL for users on logfile
$NewAcl = Get-Acl -Path $LogFile
$identity = New-Object System.Security.Principal.SecurityIdentifier S-1-5-11
$fileSystemRights = "Modify"
$type = "Allow"
$fileSystemAccessRuleArgumentList = $identity, $fileSystemRights, $type
$fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList
$NewAcl.SetAccessRule($fileSystemAccessRule)
Set-Acl -Path $LogFile -AclObject $NewAcl
}
catch {
Return $False
}
# create new file
Write-ToLog "New log file created"
# if MaxLogFiles is 0 don't delete any old archived log files
if (-not($MaxLogFiles -eq 0)) {
@ -77,8 +50,11 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
Return $True
}
}
catch {
Return $False
}
}
catch {
Return $False
}
}