Merge pull request #683 from Romanitho/fix-log-creation
Review log creation / rotationpull/688/head
commit
ee26305f5c
|
@ -34,7 +34,10 @@
|
||||||
"msiexec",
|
"msiexec",
|
||||||
"VERYSILENT",
|
"VERYSILENT",
|
||||||
"SUPPRESSMSGBOXES",
|
"SUPPRESSMSGBOXES",
|
||||||
"subfolders"
|
"subfolders",
|
||||||
|
"explorerprocesses",
|
||||||
|
"SASURL",
|
||||||
|
"LASTEXITCODE"
|
||||||
],
|
],
|
||||||
"ignorePaths": [
|
"ignorePaths": [
|
||||||
".github/*",
|
".github/*",
|
||||||
|
|
|
@ -272,7 +272,7 @@ function Add-WAUWhiteList ($AppID) {
|
||||||
}
|
}
|
||||||
Write-ToLog "-> Add $AppID to WAU included_apps.txt"
|
Write-ToLog "-> Add $AppID to WAU included_apps.txt"
|
||||||
#Add App to "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
|
#Remove duplicate and blank lines
|
||||||
$file = Get-Content $WhiteList | Select-Object -Unique | Where-Object { $_.trim() -ne "" } | Sort-Object
|
$file = Get-Content $WhiteList | Select-Object -Unique | Where-Object { $_.trim() -ne "" } | Sort-Object
|
||||||
$file | Out-File $WhiteList
|
$file | Out-File $WhiteList
|
||||||
|
@ -314,7 +314,7 @@ $Script:IsElevated = $CurrentPrincipal.IsInRole([Security.Principal.WindowsBuilt
|
||||||
$WAURegKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate\"
|
$WAURegKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate\"
|
||||||
$Script:WAUInstallLocation = Get-ItemProperty $WAURegKey -ErrorAction SilentlyContinue | Select-Object -ExpandProperty InstallLocation
|
$Script:WAUInstallLocation = Get-ItemProperty $WAURegKey -ErrorAction SilentlyContinue | Select-Object -ExpandProperty InstallLocation
|
||||||
|
|
||||||
#LogPath initialisation
|
#LogPath initialization
|
||||||
if (!($LogPath)) {
|
if (!($LogPath)) {
|
||||||
#If LogPath is not set, get WAU log path
|
#If LogPath is not set, get WAU log path
|
||||||
if ($WAUInstallLocation) {
|
if ($WAUInstallLocation) {
|
||||||
|
@ -326,7 +326,7 @@ if (!($LogPath)) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Logs initialisation
|
#Logs initialization
|
||||||
if (!(Test-Path $LogPath)) {
|
if (!(Test-Path $LogPath)) {
|
||||||
New-Item -ItemType Directory -Force -Path $LogPath | Out-Null
|
New-Item -ItemType Directory -Force -Path $LogPath | Out-Null
|
||||||
}
|
}
|
||||||
|
@ -372,7 +372,7 @@ if ($IsElevated -eq $True) {
|
||||||
$null = Update-Winget
|
$null = Update-Winget
|
||||||
#Reload Winget command
|
#Reload Winget command
|
||||||
$Script:Winget = Get-WingetCmd
|
$Script:Winget = Get-WingetCmd
|
||||||
#Run Scope Machine funtion
|
#Run Scope Machine function
|
||||||
Add-ScopeMachine
|
Add-ScopeMachine
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -11,8 +11,9 @@ Get-ChildItem "$WorkingDir\functions" -File -Filter "*.ps1" -Depth 0 | ForEach-O
|
||||||
#Config console output encoding
|
#Config console output encoding
|
||||||
$null = cmd /c ''
|
$null = cmd /c ''
|
||||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||||
|
$Script:ProgressPreference = 'SilentlyContinue'
|
||||||
|
|
||||||
#Log initialisation
|
#Log initialization
|
||||||
$LogFile = "$WorkingDir\logs\updates.log"
|
$LogFile = "$WorkingDir\logs\updates.log"
|
||||||
|
|
||||||
#Check if running account is system or interactive logon
|
#Check if running account is system or interactive logon
|
||||||
|
@ -24,7 +25,7 @@ $Script:SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId
|
||||||
if ($IsSystem) {
|
if ($IsSystem) {
|
||||||
#If log file doesn't exist, force create it
|
#If log file doesn't exist, force create it
|
||||||
if (!(Test-Path -Path $LogFile)) {
|
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
|
# Check if Intune Management Extension Logs folder exists
|
||||||
if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue)) {
|
if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue)) {
|
||||||
|
@ -108,14 +109,14 @@ if ($IsSystem) {
|
||||||
#LogRotation if System
|
#LogRotation if System
|
||||||
$LogRotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize
|
$LogRotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize
|
||||||
if ($LogRotate -eq $False) {
|
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
|
#Run post update actions if necessary if run as System
|
||||||
if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) {
|
if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) {
|
||||||
Invoke-PostUpdateActions
|
Invoke-PostUpdateActions
|
||||||
}
|
}
|
||||||
#Run Scope Machine funtion if run as System
|
#Run Scope Machine function if run as System
|
||||||
Add-ScopeMachine
|
Add-ScopeMachine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,10 +295,9 @@ if (Test-Network) {
|
||||||
Write-ToLog "Checking application updates on Winget Repository..." "yellow"
|
Write-ToLog "Checking application updates on Winget Repository..." "yellow"
|
||||||
$outdated = Get-WingetOutdatedApps
|
$outdated = Get-WingetOutdatedApps
|
||||||
|
|
||||||
#If something unusual happened
|
#If something unusual happened or no update found
|
||||||
if ($outdated -like "An unusual*") {
|
if ($outdated -like "No update found.*") {
|
||||||
Write-ToLog "$outdated" "cyan"
|
Write-ToLog "$outdated" "cyan"
|
||||||
$outdated = $False
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#Run only if $outdated is populated!
|
#Run only if $outdated is populated!
|
||||||
|
@ -401,7 +401,7 @@ if (Test-Network) {
|
||||||
Write-ToLog "No explorer process found / Nobody interactively logged on..."
|
Write-ToLog "No explorer process found / Nobody interactively logged on..."
|
||||||
}
|
}
|
||||||
Else {
|
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..."
|
Write-ToLog "User logged on, get a list of installed Winget apps in System context..."
|
||||||
Get-WingetSystemApps
|
Get-WingetSystemApps
|
||||||
|
|
||||||
|
|
|
@ -9,11 +9,11 @@ function Get-WingetOutdatedApps {
|
||||||
}
|
}
|
||||||
|
|
||||||
#Get list of available upgrades on winget format
|
#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)
|
#Start Conversion of winget format to an array. Check if "-----" exists (Winget Error Handling)
|
||||||
if (!($upgradeResult -match "-----")) {
|
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
|
#Split winget output to lines
|
||||||
|
|
|
@ -1,17 +1,9 @@
|
||||||
#Function to rotate the logs
|
#Function to rotate the logs
|
||||||
|
|
||||||
function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
|
function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
|
||||||
<#
|
|
||||||
.SYNOPSIS
|
# if MaxLogFiles is 1 just keep the original one and let it grow
|
||||||
Handle log rotation.
|
if (-not($MaxLogFiles -eq 1)) {
|
||||||
.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 {
|
try {
|
||||||
# get current size of log file
|
# get current size of log file
|
||||||
|
@ -23,33 +15,15 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
|
||||||
$logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName)
|
$logFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($logFileName)
|
||||||
$logFileNameExtension = [System.IO.Path]::GetExtension($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) {
|
if ($currentSize -ge $MaxLogSize) {
|
||||||
|
|
||||||
# construct name of archived log file
|
# construct name of archived log file
|
||||||
$newLogFileName = $logFileNameWithoutExtension + (Get-Date -Format 'yyyyMMddHHmmss').ToString() + $logFileNameExtension
|
$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
|
# create new file
|
||||||
Copy-Item -Path $LogFile -Destination (Join-Path (Split-Path $LogFile) $newLogFileName)
|
Write-ToLog "New log file created"
|
||||||
|
|
||||||
# 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
|
|
||||||
}
|
|
||||||
|
|
||||||
# if MaxLogFiles is 0 don't delete any old archived log files
|
# if MaxLogFiles is 0 don't delete any old archived log files
|
||||||
if (-not($MaxLogFiles -eq 0)) {
|
if (-not($MaxLogFiles -eq 0)) {
|
||||||
|
@ -77,8 +51,11 @@ function Invoke-LogRotation ($LogFile, $MaxLogFiles, $MaxLogSize) {
|
||||||
Return $True
|
Return $True
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch {
|
catch {
|
||||||
Return $False
|
Return $False
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue