From b64e18779f6e02fdaf93118c62b65b35d3e0efc0 Mon Sep 17 00:00:00 2001 From: Olai Date: Sun, 26 May 2024 17:00:16 +0200 Subject: [PATCH] Feature: Added Auto Renamer Added the ability to rename the folders to the newest version of Chrome --- .gitignore | 2 + Chrome Downloader.ps1 | 4 ++ README.md | 4 +- Rename.ps1 | 92 +++++++++++++++++++++++++++++++++++++++++++ config.json | 3 +- 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Rename.ps1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0f297d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +/Template \ No newline at end of file diff --git a/Chrome Downloader.ps1 b/Chrome Downloader.ps1 index 13b9ddd..185247f 100644 --- a/Chrome Downloader.ps1 +++ b/Chrome Downloader.ps1 @@ -127,3 +127,7 @@ if ($config.options.enableForcedVersion) { } } } + +if ($config.options.enableNumberedVersion) { + & $PSScriptRoot\Rename.ps1 +} \ No newline at end of file diff --git a/README.md b/README.md index 7bf1a70..07d9c21 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ The script reads its configuration from a JSON file named `config.json` located - `enableRegularVersion`: Boolean flag to enable the downloading of the regular version of Chrome. - `enableForcedVersion`: Boolean flag to enable the downloading of the forced update version of Chrome. +- `enableNumberedVersion`: Boolean flag to enable the automatic renaming of the folder to the newest version of Chrome. - `logDateFormat`: A string defining the format of timestamps in log messages. #### Date Configuration @@ -62,7 +63,8 @@ Output: 29-06-2024 15:19:30 { "options": { "enableRegularVersion": true, - "enableForcedVersion": false + "enableForcedVersion": false, + "enableNumberedVersion": false }, "logDateFormat": "yyyy'/'MM'/'dd hh:mm:ss tt" } diff --git a/Rename.ps1 b/Rename.ps1 new file mode 100644 index 0000000..a30ddf3 --- /dev/null +++ b/Rename.ps1 @@ -0,0 +1,92 @@ +# Read configuration from JSON file +$configPath = "$PSScriptRoot\config.json" +$config = Get-Content -Path $configPath | ConvertFrom-Json + +$destinationFolder = Join-Path -Path $PSScriptRoot -ChildPath "Chrome - VERSION" +$forceUpdateFolder = Join-Path -Path $PSScriptRoot -ChildPath "Chrome - VERSION_force_update" + +function Log-Message { + param ( + [string]$message + ) + $timestamp = Get-Date -Format $dateFormat + Write-Output "[$timestamp] - $message" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8 +} + +if ($config.options.enableRegularVersion -and -not $config.options.enableForcedVersion) { + $msiPath = "$PSScriptRoot\Chrome - VERSION\Files\googlechromestandaloneenterprise64.msi" + Start-Process -FilePath "msiexec.exe" -ArgumentList "/i `"$msiPath`" /quiet" -Wait + $chromeRegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + $chromeVersion = Get-ChildItem -Path $chromeRegPath | + Get-ItemProperty | + Where-Object { $_.DisplayName -like "*Google Chrome*" } | + Select-Object -ExpandProperty DisplayVersion + # Rename the folder if the version was retrieved + if ($chromeVersion) { + $newFolderName = "Chrome - $chromeVersion" + $newFolderPath = Join-Path -Path $PSScriptRoot -ChildPath $newFolderName + try { + Rename-Item -Path $destinationFolder -NewName $newFolderName -ErrorAction Stop + Log-Message "Success: Folder renamed to $newFolderName" + } catch { + Log-Message "Error: Failed to rename folder - $_" + } + } else { + Log-Message "Warning: Chrome version could not be determined. Folder was not renamed." + } +} +elseif ($config.options.enableForcedVersion -and -not $config.options.enableRegularVersion) { + $msiPath = "$PSScriptRoot\Chrome - VERSION_force_update\googlechromestandaloneenterprise64.msi" + Start-Process -FilePath "msiexec.exe" -ArgumentList "/i `"$msiPath`" /quiet" -Wait + $chromeRegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + $chromeVersion = Get-ChildItem -Path $chromeRegPath | + Get-ItemProperty | + Where-Object { $_.DisplayName -like "*Google Chrome*" } | + Select-Object -ExpandProperty DisplayVersion + # Rename the folder if the version was retrieved + if ($chromeVersion) { + $newFolderName = "Chrome - $chromeVersion" + "_force_update" + $newFolderPath = Join-Path -Path $PSScriptRoot -ChildPath $newFolderName + try { + Rename-Item -Path $forceUpdateFolder -NewName $newFolderName -ErrorAction Stop + Log-Message "Success: Folder renamed to $newFolderName" + } catch { + Log-Message "Error: Failed to rename folder - $_" + } + } else { + Log-Message "Warning: Chrome version could not be determined. Folder was not renamed." + } +} +elseif ($config.options.enableForcedVersion -and $config.options.enableRegularVersion) { + $msiPath = "$PSScriptRoot\Chrome - VERSION_force_update\googlechromestandaloneenterprise64.msi" + Start-Process -FilePath "msiexec.exe" -ArgumentList "/i `"$msiPath`" /quiet" -Wait + + $chromeRegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + $chromeVersion = Get-ChildItem -Path $chromeRegPath | + Get-ItemProperty | + Where-Object { $_.DisplayName -like "*Google Chrome*" } | + Select-Object -ExpandProperty DisplayVersion + + # Rename both folders if the version was retrieved + if ($chromeVersion) { + # Regular version folder + $newRegularFolderName = "Chrome - $chromeVersion" + try { + Rename-Item -Path $destinationFolder -NewName $newRegularFolderName -ErrorAction Stop + Log-Message "Success: Folder renamed to $newRegularFolderName" + } catch { + Log-Message "Error: Failed to rename folder - $_" + } + + # Forced version folder + $newForcedFolderName = "Chrome - $chromeVersion" + "_force_update" + try { + Rename-Item -Path $forceUpdateFolder -NewName $newForcedFolderName -ErrorAction Stop + Log-Message "Success: Folder renamed to $newForcedFolderName" + } catch { + Log-Message "Error: Failed to rename folder - $_" + } + } else { + Log-Message "Warning: Chrome version could not be determined. Folders were not renamed." + } +} diff --git a/config.json b/config.json index 28ca3ed..38da536 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,8 @@ { "options": { "enableRegularVersion": true, - "enableForcedVersion": false + "enableForcedVersion": false, + "enableNumberedVersion": false }, "logDateFormat": "dd/MM/yyyy HH:mm:ss" } \ No newline at end of file