@@ -20,7 +20,6 @@ function Log-Message {
2020 Add-Content - Path $logPath - Value $logEntry
2121}
2222
23- # Function to update the script
2423function Update-Script {
2524 param (
2625 [string ]$RemoteScriptURL = " https://raw.githubusercontent.com/Dantdmnl/Network_Configuration_Script/refs/heads/main/Network_Configuration.ps1" ,
@@ -46,25 +45,42 @@ function Update-Script {
4645
4746 # Ensure the version file exists
4847 if (-not (Test-Path $versionFilePath )) {
49- Write-Host " Version file not found. Creating a new one with version 0.0." - ForegroundColor Yellow
50- Set-Content - Path $versionFilePath - Value " 0.0"
48+ Write-Host " Version file not found. Creating a new one with version 1.0." - ForegroundColor Yellow
49+ Log- Message " Version file not found. Creating a new one with version 1.0."
50+ Set-Content - Path $versionFilePath - Value " 1.0"
5151 }
5252
53- $currentVersion = Get-Content $versionFilePath
53+ $currentVersion = ( Get-Content $versionFilePath ).Trim()
5454
5555 try {
56- # Fetch the remote version
56+ # Fetch the remote script content
5757 $RemoteScriptContent = Invoke-WebRequest - Uri $RemoteScriptURL - UseBasicParsing
5858 if (-not $RemoteScriptContent ) {
5959 Write-Host " Failed to fetch the remote script. Please check the URL." - ForegroundColor Red
60- Log- Message " Failed to fetch the remote script. Please check the URL."
60+ Log- Message " Failed to fetch the remote script. Please check the URL." " ERROR "
6161 return
6262 }
6363
64- # Parse the remote version (assume the version is in the first comment line, modify if needed)
65- $RemoteVersion = ($RemoteScriptContent.Content -split " `n " | Select-String - Pattern " # Version:" ) -replace " # Version:" , " " -as [string ]
66- $RemoteVersion = $RemoteVersion.Trim ()
64+ # Extract the version line from the remote script
65+ $VersionLine = ($RemoteScriptContent.Content -split " `n " | Select-Object - First 10 | Where-Object { $_ -match " # Version:" })
6766
67+ if ($VersionLine ) {
68+ # Strict regex to extract only the version number
69+ $RemoteVersion = ($VersionLine -replace " .*# Version:\s*([0-9]+\.[0-9]+).*" , ' $1' ).Trim()
70+ } else {
71+ Write-Host " Could not find a valid version in the remote script." - ForegroundColor Red
72+ Log- Message " Could not find a valid version in the remote script." " ERROR"
73+ return
74+ }
75+
76+ # Ensure the version is valid
77+ if (-not $RemoteVersion -or $RemoteVersion -notmatch " ^\d+\.\d+$" ) {
78+ Write-Host " Invalid version format in the remote script." - ForegroundColor Red
79+ Log- Message " Invalid version format in the remote script." " ERROR"
80+ return
81+ }
82+
83+ # Compare versions
6884 if ($RemoteVersion -ne $currentVersion ) {
6985 Write-Host " An updated version of the script is available (Current: $currentVersion , Remote: $RemoteVersion )." - ForegroundColor Cyan
7086 Log- Message " An updated version of the script is available (Current: $currentVersion , Remote: $RemoteVersion )."
@@ -76,6 +92,7 @@ function Update-Script {
7692 $BackupPath = " $CurrentScriptPath .bak"
7793 Copy-Item - Path $CurrentScriptPath - Destination $BackupPath - Force
7894 Write-Host " A backup of the current script has been saved as $BackupPath ." - ForegroundColor Yellow
95+ Log- Message " A backup of the current script has been saved as $BackupPath ."
7996
8097 # Update the script
8198 $RemoteScriptContent.Content | Set-Content - Path $CurrentScriptPath - Force
0 commit comments