From 673e4a2f1759f452d6c36e6517979a131e244c3d Mon Sep 17 00:00:00 2001 From: Andreas Lindemark <andreas.lindemark@liu.se> Date: Mon, 10 Mar 2025 18:34:05 +0100 Subject: [PATCH] Modified some variables, fixed issue with transcript path and busy.flag --- Windows/Chocolatey/ChocolateyUpgrade.ps1 | 35 ++++++++++++------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Windows/Chocolatey/ChocolateyUpgrade.ps1 b/Windows/Chocolatey/ChocolateyUpgrade.ps1 index 7225ffa..a460815 100644 --- a/Windows/Chocolatey/ChocolateyUpgrade.ps1 +++ b/Windows/Chocolatey/ChocolateyUpgrade.ps1 @@ -12,10 +12,10 @@ function Remove-OldLogFiles { param ( [string]$logPath, [int]$maxFiles, - [string]$scriptNameWithoutExtension + [string]$scriptName ) # Get all log files for the current script - $logFiles = Get-ChildItem -Path $logPath -Filter "$scriptNameWithoutExtension_*.log" | Sort-Object LastWriteTime -Descending + $logFiles = Get-ChildItem -Path $logPath -Filter "$scriptName_*.log" | Sort-Object LastWriteTime -Descending # Check if the number of log files exceeds the maximum allowed if ($logFiles.Count -gt $maxFiles) { @@ -30,6 +30,18 @@ function Remove-OldLogFiles { <#== Script starts here ==#> Write-Host "Starting script execution..." +<#== Get current date and time ==#> +Write-Host "Getting current date and time..." +$currentDateTime = Get-Date -Format "yyyy-MM-dd_HHmm" + +# Determine the name of the currently executing script without the extension +$scriptName = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name) + +<#== Start Transcript ==#> +Write-Host "Starting transcript..." +$transcriptPath = "C:\Install Logs\$scriptName`_$currentDateTime.log" +Start-Transcript -Path $transcriptPath + # Define the busy.flag file path $busyFlagPath = "C:\Install Logs\ChocolateyUpgrade_busy.flag" @@ -41,22 +53,9 @@ if (Test-Path $busyFlagPath) { # Create the busy.flag file and write start time and script information $startTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss" -$scriptName = $MyInvocation.MyCommand.Name $busyFlagContent = "Script: $scriptName`nStart Time: $startTime" -New-Item -Path $busyFlagPath -ItemType File -Force > null -Set-Content -Path $busyFlagPath -Value $busyFlagContent > null - -<#== Get current date and time ==#> -Write-Host "Getting current date and time..." -$currentDateTime = Get-Date -Format "yyyy-MM-dd_HHmm" - -# Determine the name of the currently executing script without the extension -$scriptNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name) - -<#== Start Transcript ==#> -Write-Host "Starting transcript..." -$transcriptPath = "C:\Install Logs\$scriptNameWithoutExtension_$currentDateTime.log" -Start-Transcript -Path $transcriptPath +New-Item -Path $busyFlagPath -ItemType File -Force > $null +Set-Content -Path $busyFlagPath -Value $busyFlagContent > $null <#== Apply random delay if enabled ==#> if ($EnableRandomDelay) { @@ -73,7 +72,7 @@ Write-Host "Chocolatey upgrade completed." <#== Remove old log files ==#> Write-Host "Removing old log files..." -Remove-OldLogFiles -logPath "C:\Install Logs" -maxFiles 45 -scriptNameWithoutExtension $scriptNameWithoutExtension +Remove-OldLogFiles -logPath "C:\Install Logs" -maxFiles 45 -scriptName $scriptName <#== Stop Transcript ==#> Write-Host "Stopping transcript..." -- GitLab