Created
April 23, 2024 09:34
-
-
Save niko-la-petrovic/31968f05d1973bb8cf2b33be56f9f691 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Replaces a target file with a reference file if the reference file has a newer file version. | |
.DESCRIPTION | |
The function replaces a target file with a reference file if the reference file has a newer file version. | |
.PARAMETER ReferencePath | |
The path to the reference file. This file will be used to determine whether the target file should be replaced. | |
.PARAMETER TargetPath | |
The path to the target file. This file will be replaced with the reference file if the reference file has a newer file version. | |
.PARAMETER DryRun | |
If this switch is set, the function will not replace the target file with the reference file. Instead, it will output a message indicating whether the reference file has a newer file version than the target file. | |
.EXAMPLE | |
Replace-ByNewerFileVersion -TargetPath "C:\Program Files\MyApp\MyApp.exe" -ReferencePath "C:\Downloads\MyApp.exe" | |
Replaces the target file "C:\Program Files\MyApp\MyApp.exe" with the reference file "C:\Downloads\MyApp.exe" if the reference file has a newer file version. | |
#> | |
function Replace-ByNewerFileVersion { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$ReferencePath, | |
[Parameter(Mandatory = $true)] | |
[string]$TargetPath, | |
[Parameter(Mandatory = $false)] | |
[switch]$DryRun, | |
[Parameter(Mandatory = $false)] | |
[switch]$TerminateBeforeReplace | |
) | |
$referenceVersion = [System.Version](Get-Item $ReferencePath).VersionInfo.FileVersion | |
$targetFile = Get-Item $TargetPath | |
$targetVersion = [System.Version]($targetFile).VersionInfo.FileVersion | |
Write-Host "Reference file version: $referenceVersion" | |
Write-Host "Target file version: $targetVersion" | |
$isReferenceVersionNewer = $referenceVersion -gt $targetVersion | |
$runningProcesses = Get-Process | Where-Object { $_.Path -eq $targetFile.FullName } | |
if ($runningProcesses.Count -eq 0) { | |
Write-Output "No running processes found for the target file." | |
} | |
else { | |
Write-Host "Running processes of the target file:" | |
$runningProcesses | Select-Object -Property Id, Name, Path | |
} | |
if ($DryRun) { | |
if ($isReferenceVersionNewer) { | |
Write-Output "The reference file has a newer file version than the target file." | |
} | |
else { | |
Write-Output "The reference file does not have a newer file version than the target file." | |
} | |
return $null | |
} | |
if ($isReferenceVersionNewer) { | |
if ($TerminateBeforeReplace) { | |
Write-Output "The reference file has a newer file version than the target file. Terminating the process before replacing the target file." | |
Write-Host "Terminating running processes for the target file" | |
$runningProcesses | Stop-Process -Force | |
Start-Sleep -Seconds 3 | |
} | |
Copy-Item -Path $ReferencePath -Destination $TargetPath -Force | |
Write-Output "The target file has been replaced with the reference file." | |
} | |
else { | |
Write-Output "The target file has not been replaced with the reference file because the reference file does not have a newer file version." | |
} | |
return $null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment