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
This commit is contained in:
parent
6e7746aab4
commit
d3d553aaf6
4 changed files with 38 additions and 13 deletions
|
@ -3,18 +3,23 @@ $configPath = "$PSScriptRoot\config.json"
|
||||||
$config = Get-Content -Path $configPath | ConvertFrom-Json
|
$config = Get-Content -Path $configPath | ConvertFrom-Json
|
||||||
|
|
||||||
# Get the date format from the configuration, or use the default format if not provided
|
# 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) {
|
if (-not $dateFormat) {
|
||||||
$dateFormat = "dd/MM/yyyy HH:mm:ss"
|
$dateFormat = "dd/MM/yyyy HH:mm:ss"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Function to log messages with the specified date format
|
# Function to log messages with the specified date format
|
||||||
|
$logFileName = $config.logging.fileName
|
||||||
|
if (-not $logFileName) {
|
||||||
|
$logFileName = "chrome_downloader.log"
|
||||||
|
}
|
||||||
function Log-Message {
|
function Log-Message {
|
||||||
param (
|
param (
|
||||||
[string]$message
|
[string]$message
|
||||||
)
|
)
|
||||||
$timestamp = Get-Date -Format $dateFormat
|
$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
|
# 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
|
# Check if the script is running with administrative privileges
|
||||||
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
|
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 {
|
else {
|
||||||
& $PSScriptRoot\Rename.ps1
|
& $PSScriptRoot\Rename.ps1
|
||||||
|
|
16
README.md
16
README.md
|
@ -15,19 +15,25 @@ The `config.json` file should be structured as follows:
|
||||||
"options": {
|
"options": {
|
||||||
"enableRegularVersion": true,
|
"enableRegularVersion": true,
|
||||||
"enableForcedVersion": false,
|
"enableForcedVersion": false,
|
||||||
"enableNumberedVersion": false,
|
"folderNumberedVersion": false,
|
||||||
"checkExist": false,
|
"checkExist": false
|
||||||
},
|
},
|
||||||
|
"logging": {
|
||||||
|
"fileName": "chrome_downloader.log",
|
||||||
"logDateFormat": "dd/MM/yyyy HH:mm:ss"
|
"logDateFormat": "dd/MM/yyyy HH:mm:ss"
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- `enableRegularVersion`: A boolean flag to enable downloading and installing the regular version of Chrome.
|
- `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.
|
- `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.** ⚠️
|
- `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`.
|
- `logDateFormat`: A string defining the format of timestamps in logs. The default format is `dd/MM/yyyy HH:mm:ss`.
|
||||||
|
|
||||||
|
|
||||||
### Date Configuration
|
### Date Configuration
|
||||||
|
|
||||||
##### `yyyy`: This specifier represents the year portion of the date. It uses four digits to represent the year. For example, 2024.
|
##### `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: <code>06.29.2024 15:19:30</code>
|
||||||
Output: <code>29-06-2024 15:19:30</code>
|
Output: <code>29-06-2024 15:19:30</code>
|
||||||
|
|
||||||
### Numbered Version
|
### 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:
|
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
|
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
|
## Script Usage
|
||||||
### 1. Prepare the Environment:
|
### 1. Prepare the Environment:
|
||||||
|
|
13
Rename.ps1
13
Rename.ps1
|
@ -2,15 +2,26 @@
|
||||||
$configPath = "$PSScriptRoot\config.json"
|
$configPath = "$PSScriptRoot\config.json"
|
||||||
$config = Get-Content -Path $configPath | ConvertFrom-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"
|
$destinationFolder = Join-Path -Path $PSScriptRoot -ChildPath "Chrome - VERSION"
|
||||||
$forceUpdateFolder = Join-Path -Path $PSScriptRoot -ChildPath "Chrome - VERSION_force_update"
|
$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 {
|
function Log-Message {
|
||||||
param (
|
param (
|
||||||
[string]$message
|
[string]$message
|
||||||
)
|
)
|
||||||
$timestamp = Get-Date -Format $dateFormat
|
$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) {
|
if ($config.options.enableRegularVersion -and -not $config.options.enableForcedVersion) {
|
||||||
|
|
|
@ -2,8 +2,11 @@
|
||||||
"options": {
|
"options": {
|
||||||
"enableRegularVersion": true,
|
"enableRegularVersion": true,
|
||||||
"enableForcedVersion": false,
|
"enableForcedVersion": false,
|
||||||
"enableNumberedVersion": false,
|
"folderNumberedVersion": false,
|
||||||
"checkExist": false
|
"checkExist": false
|
||||||
},
|
},
|
||||||
|
"logging": {
|
||||||
|
"fileName": "chrome_downloader.log",
|
||||||
"logDateFormat": "dd/MM/yyyy HH:mm:ss"
|
"logDateFormat": "dd/MM/yyyy HH:mm:ss"
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue