From d3d553aaf6ff1a2197bdcf748d065101eb38045d Mon Sep 17 00:00:00 2001 From: Olai Date: Tue, 28 May 2024 14:38:05 +0200 Subject: [PATCH] Feature: Change name of log file. Logging: Added the opportunity to change the name of the log file in JSON. Numbered Version: "enableNumberedVersion", just got renamed to "folderNumberedVersion" to make it more easily understandable --- Chrome Downloader.ps1 | 13 +++++++++---- README.md | 18 ++++++++++++------ Rename.ps1 | 13 ++++++++++++- config.json | 7 +++++-- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/Chrome Downloader.ps1 b/Chrome Downloader.ps1 index 7105335..d93a319 100644 --- a/Chrome Downloader.ps1 +++ b/Chrome Downloader.ps1 @@ -3,18 +3,23 @@ $configPath = "$PSScriptRoot\config.json" $config = Get-Content -Path $configPath | ConvertFrom-Json # Get the date format from the configuration, or use the default format if not provided -$dateFormat = $config.logDateFormat +$dateFormat = $config.logging.logDateFormat if (-not $dateFormat) { $dateFormat = "dd/MM/yyyy HH:mm:ss" } + # Function to log messages with the specified date format +$logFileName = $config.logging.fileName +if (-not $logFileName) { + $logFileName = "chrome_downloader.log" +} function Log-Message { param ( [string]$message ) $timestamp = Get-Date -Format $dateFormat - Write-Output "[$timestamp] - $message" | Out-File -Append -FilePath "$PSScriptRoot\chrome_downloader.log" -Encoding utf8 + Write-Output "[$timestamp] - $message" | Out-File -Append -FilePath "$PSScriptRoot\$logFileName" -Encoding utf8 } # Log the start of the script @@ -149,10 +154,10 @@ if ($config.options.enableForcedVersion) { } } -if ($config.options.enableNumberedVersion) { +if ($config.options.folderNumberedVersion) { # Check if the script is running with administrative privileges if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { - Log-Message "Error: the config 'enableNumberedVersion' requires administrative privileges to run." + Log-Message "Error: the config 'folderNumberedVersion' requires administrative privileges to run." } else { & $PSScriptRoot\Rename.ps1 diff --git a/README.md b/README.md index da9d6e8..b8e61bb 100644 --- a/README.md +++ b/README.md @@ -15,19 +15,25 @@ The `config.json` file should be structured as follows: "options": { "enableRegularVersion": true, "enableForcedVersion": false, - "enableNumberedVersion": false, - "checkExist": false, + "folderNumberedVersion": false, + "checkExist": false }, - "logDateFormat": "dd/MM/yyyy HH:mm:ss" + "logging": { + "fileName": "chrome_downloader.log", + "logDateFormat": "dd/MM/yyyy HH:mm:ss" + + } } ``` - `enableRegularVersion`: A boolean flag to enable downloading and installing the regular version of Chrome. - `enableForcedVersion`: A boolean flag to enable downloading and installing the forced update version of Chrome. -- `enableNumberedVersion`: A boolean flag to enable the automatic renaming of the folder to the newest version of Chrome. ⚠️ **This option requires administrative privileges when executing the script!** ⚠️ +- `folderNumberedVersion`: A boolean flag to enable the automatic renaming of the folder to the newest version of Chrome. ⚠️ **This option requires administrative privileges when executing the script!** ⚠️ - `checkExist`: A boolean flag to delete old Chrome folders when the script is executed. ⚠️ **This action will delete your Chrome folders, so ensure you have backups if you wish to retain them.** ⚠️ +- `fileName`: A string defining the name of the log file. The default format is `chrome_downloader.log`. - `logDateFormat`: A string defining the format of timestamps in logs. The default format is `dd/MM/yyyy HH:mm:ss`. + ### Date Configuration ##### `yyyy`: This specifier represents the year portion of the date. It uses four digits to represent the year. For example, 2024. @@ -72,7 +78,7 @@ Output: 06.29.2024 15:19:30 Output: 29-06-2024 15:19:30 ### Numbered Version -`enableNumberedVersion`: Set this to `true` to enable automatic renaming of the folder based on the downloaded Chrome version. This action requires administrative privileges. +`folderNumberedVersion`: Set this to `true` to enable automatic renaming of the folder based on the downloaded Chrome version. This action requires administrative privileges. For example, if this option is enabled, the folders will be named as follows: @@ -86,7 +92,7 @@ Chrome - VERSION_force_update Chrome - 125.0.6422.113_force_update ``` -The `enableNumberedVersion` configuration requires administrative privileges because the only way to obtain the Chrome version number is by installing the MSI file and retrieving the version from the Windows registry. +The `folderNumberedVersion` configuration requires administrative privileges because the only way to obtain the Chrome version number is by installing the MSI file and retrieving the version from the Windows registry. ## Script Usage ### 1. Prepare the Environment: diff --git a/Rename.ps1 b/Rename.ps1 index cc8a45f..21a745e 100644 --- a/Rename.ps1 +++ b/Rename.ps1 @@ -2,15 +2,26 @@ $configPath = "$PSScriptRoot\config.json" $config = Get-Content -Path $configPath | ConvertFrom-Json +# Get the date format from the configuration, or use the default format if not provided +$dateFormat = $config.logging.logDateFormat +if (-not $dateFormat) { + $dateFormat = "dd/MM/yyyy HH:mm:ss" +} + $destinationFolder = Join-Path -Path $PSScriptRoot -ChildPath "Chrome - VERSION" $forceUpdateFolder = Join-Path -Path $PSScriptRoot -ChildPath "Chrome - VERSION_force_update" +# Function to log messages with the specified date format +$logFileName = $config.logging.fileName +if (-not $logFileName) { + $logFileName = "chrome_downloader.log" +} function Log-Message { param ( [string]$message ) $timestamp = Get-Date -Format $dateFormat - Write-Output "[$timestamp] - $message" | Out-File -Append -FilePath "$PSScriptRoot\chrome_downloader.log" -Encoding utf8 + Write-Output "[$timestamp] - $message" | Out-File -Append -FilePath "$PSScriptRoot\$logFileName" -Encoding utf8 } if ($config.options.enableRegularVersion -and -not $config.options.enableForcedVersion) { diff --git a/config.json b/config.json index b8756d0..39b0c9b 100644 --- a/config.json +++ b/config.json @@ -2,8 +2,11 @@ "options": { "enableRegularVersion": true, "enableForcedVersion": false, - "enableNumberedVersion": false, + "folderNumberedVersion": false, "checkExist": false }, - "logDateFormat": "dd/MM/yyyy HH:mm:ss" + "logging": { + "fileName": "chrome_downloader.log", + "logDateFormat": "dd/MM/yyyy HH:mm:ss" + } } \ No newline at end of file