From d8f15941cef1903c8accf19e59411e4eb8772bf8 Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Tue, 5 Dec 2017 16:49:45 +0100 Subject: [PATCH] Wrap renaming with a try-catch block. Refers to #13 --- AutoTx/Resources/Update-AutoTxService.ps1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/AutoTx/Resources/Update-AutoTxService.ps1 b/AutoTx/Resources/Update-AutoTxService.ps1 index 382e6bb..c6d316d 100644 --- a/AutoTx/Resources/Update-AutoTxService.ps1 +++ b/AutoTx/Resources/Update-AutoTxService.ps1 @@ -105,8 +105,15 @@ function Update-FileIfNewer([string]$SourcePath, [string]$Destination) { # assemble a timestamp string like "2017-12-04T16.41.35" $BakTimeStamp = Get-Date -Format s | foreach {$_ -replace ":", "."} $BakName = "$($SrcFileNoSuffix)_pre-$BakTimeStamp$SrcFileSuffix" - Rename-Item "$DstPath" "$Destination\$BakName" Log-Info "Creating backup of '$($DstPath)' to '$($BakName)'." + try { + Rename-Item "$DstPath" "$Destination\$BakName" -ErrorAction Stop + } + catch { + $ex = $_.Exception.Message + Log-Error "Backing up '$($DstPath)' as '$($BakName) FAILED!`n$($ex)" + Exit + } try { Copy-Item -Path $SourcePath -Destination $Destination -ErrorAction Stop Log-Info "Updated config file '$($DstPath)'." -- GitLab