Skip to content

Commit

Permalink
Merge pull request #683 from Romanitho/fix-log-creation
Browse files Browse the repository at this point in the history
Review log creation / rotation
  • Loading branch information
Romanitho committed Sep 2, 2024
2 parents d0334a8 + df13512 commit ee26305
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 64 deletions.
7 changes: 5 additions & 2 deletions .github/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
"msiexec",
"VERYSILENT",
"SUPPRESSMSGBOXES",
"subfolders"
"subfolders",
"explorerprocesses",
"SASURL",
"LASTEXITCODE"
],
"ignorePaths": [
".github/*",
Expand All @@ -46,4 +49,4 @@
"*.csv",
"*.txt"
]
}
}
8 changes: 4 additions & 4 deletions Sources/Winget-AutoUpdate/Winget-Install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -326,7 +326,7 @@ if (!($LogPath)) {
}
}

#Logs initialisation
#Logs initialization
if (!(Test-Path $LogPath)) {
New-Item -ItemType Directory -Force -Path $LogPath | Out-Null
}
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions Sources/Winget-AutoUpdate/Winget-Upgrade.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 25 additions & 48 deletions Sources/Winget-AutoUpdate/functions/Invoke-LogRotation.ps1
Original file line number Diff line number Diff line change
@@ -1,55 +1,29 @@
#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
#>

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 MaxLogFiles is 1 just keep the original one and let it grow
if (-not($MaxLogFiles -eq 1)) {

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 ($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)) {
Expand Down Expand Up @@ -77,8 +51,11 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
Return $True
}
}

catch {
Return $False
}

}
catch {
Return $False
}

}

0 comments on commit ee26305

Please sign in to comment.