Merge pull request #683 from Romanitho/fix-log-creation

Review log creation / rotation
pull/688/head
Romain 2024-09-03 00:31:09 +02:00 committed by GitHub
commit ee26305f5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 41 additions and 61 deletions

5
.github/cspell.json vendored
View File

@ -34,7 +34,10 @@
"msiexec",
"VERYSILENT",
"SUPPRESSMSGBOXES",
"subfolders"
"subfolders",
"explorerprocesses",
"SASURL",
"LASTEXITCODE"
],
"ignorePaths": [
".github/*",

View File

@ -272,7 +272,7 @@ function Add-WAUWhiteList ($AppID) {
}
Write-ToLog "-> Add $AppID to WAU included_apps.txt"
#Add App to "included_apps.txt"
Add-Content -path $WhiteList -Value "`n$AppID" -Force
Add-Content -Path $WhiteList -Value "`n$AppID" -Force
#Remove duplicate and blank lines
$file = Get-Content $WhiteList | Select-Object -Unique | Where-Object { $_.trim() -ne "" } | Sort-Object
$file | Out-File $WhiteList
@ -314,7 +314,7 @@ $Script:IsElevated = $CurrentPrincipal.IsInRole([Security.Principal.WindowsBuilt
$WAURegKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate\"
$Script:WAUInstallLocation = Get-ItemProperty $WAURegKey -ErrorAction SilentlyContinue | Select-Object -ExpandProperty InstallLocation
#LogPath initialisation
#LogPath initialization
if (!($LogPath)) {
#If LogPath is not set, get WAU log path
if ($WAUInstallLocation) {
@ -326,7 +326,7 @@ if (!($LogPath)) {
}
}
#Logs initialisation
#Logs initialization
if (!(Test-Path $LogPath)) {
New-Item -ItemType Directory -Force -Path $LogPath | Out-Null
}
@ -372,7 +372,7 @@ if ($IsElevated -eq $True) {
$null = Update-Winget
#Reload Winget command
$Script:Winget = Get-WingetCmd
#Run Scope Machine funtion
#Run Scope Machine function
Add-ScopeMachine
}
else {

View File

@ -11,8 +11,9 @@ 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
#Log initialization
$LogFile = "$WorkingDir\logs\updates.log"
#Check if running account is system or interactive logon
@ -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)) {
@ -108,14 +109,14 @@ if ($IsSystem) {
#LogRotation if System
$LogRotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize
if ($LogRotate -eq $False) {
Write-ToLog "An Exception occured during Log Rotation..."
Write-ToLog "An Exception occurred during Log Rotation..."
}
#Run post update actions if necessary if run as System
if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) {
Invoke-PostUpdateActions
}
#Run Scope Machine funtion if run as System
#Run Scope Machine function if run as System
Add-ScopeMachine
}
@ -294,10 +295,9 @@ if (Test-Network) {
Write-ToLog "Checking application updates on Winget Repository..." "yellow"
$outdated = Get-WingetOutdatedApps
#If something unusual happened
if ($outdated -like "An unusual*") {
#If something unusual happened or no update found
if ($outdated -like "No update found.*") {
Write-ToLog "$outdated" "cyan"
$outdated = $False
}
#Run only if $outdated is populated!
@ -401,7 +401,7 @@ if (Test-Network) {
Write-ToLog "No explorer process found / Nobody interactively logged on..."
}
Else {
#Get Winget system apps to excape them befor running user context
#Get Winget system apps to escape them before running user context
Write-ToLog "User logged on, get a list of installed Winget apps in System context..."
Get-WingetSystemApps

View File

@ -9,11 +9,11 @@ function Get-WingetOutdatedApps {
}
#Get list of available upgrades on winget format
$upgradeResult = & $Winget upgrade --source winget | Out-String
$upgradeResult = & $Winget upgrade --source winget | Where-Object { $_ -notlike " *" } | Out-String
#Start Conversion of winget format to an array. Check if "-----" exists (Winget Error Handling)
if (!($upgradeResult -match "-----")) {
return "An unusual thing happened (maybe all apps are upgraded):`n$upgradeResult"
return "No update found. Winget upgrade output:`n$upgradeResult"
}
#Split winget output to lines

View File

@ -1,17 +1,9 @@
#Function to rotate the logs
#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
#>
# if MaxLogFiles is 1 just keep the original one and let it grow
if (-not($MaxLogFiles -eq 1)) {
try {
# get current size of log file
@ -23,33 +15,15 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
$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 old log file
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 +51,11 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
Return $True
}
}
}
catch {
Return $False
}
}
}