Synopsis:
- Script file directory name -> PowerShellScripts (in Desktop in local C:...)
- Script file name -> Dbackup.ps1
- Intended source for mirroring -> D:\ (a partition on local hard disk)
- Intended destination for mirroring -> F:\D_LAPTOP_BACKUP_ ... (F: is external SSD connected via USB)
Script:
[string]$currentDirectory = Get-Location
[DateTime]$currentDateTime = Get-Date
[string]$currentTimeStamp = $currentDateTime.ToString("yyyy-MM-dd_HH-mm-ss")
[string]$currentDayStamp = $currentDateTime.ToString("yyyy-MM-dd")
[string]$sourcePath = "D:"
write-host "Source Path: " $sourcePath
[string]$destinationDrive = "F:"
[string]$destinationFolderName = "\D_LAPTOP_BACKUP_"
[string]$destinationPath = $destinationDrive + $destinationFolderName + $currentDayStamp
write-host "Destination Path: " $destinationPath
[string]$logFile = ""
[string]$logFileExtenstion = ".txt"
$logFile = $currentDirectory + $destinationFolderName + $currentTimeStamp + $logFileExtenstion
write-host "Log File: " $logFile
[string]$defaultDirExclusionSet = '$RECYCLE.BIN "D:\System Volume Information"'
write-host "Dir Excluded: " $defaultDirExclusionSet
[string]$commandString = "Robocopy $sourcePath $destinationPath /MIR /R:3 /W:3 /DCOPY:DAT /COPY:DATSOU /TEE /LOG:$logFile /MT:32 /XD $defaultDirExclusionSet"
write-host $commandString
Invoke-Expression $commandString
Issue:
When this script is run by opening PowerShell in Admin mode from PowerShellScripts directory (which is in Desktop) it runs fine.
However, when I move this PowerShellScripts directory to D:\ and this script is run by opening PowerShell in Admin mode from now moved PowerShellScripts directory, the source for mirror is no more D:\ (when the robocopy expression is run by this script). The source automatically changes to "D:\PowerShellScripts" by robocopy. Note source is hardcoded in script and $commandString prints properly on console and shows the intended correct expression and source.
I am confused. It was scary when it happened yesterday (/MIR used) but luckily destination was kept F:\D_LAPTOP_BACKUP_2022-07-24\ by robocopy. Kindly help.
Result Header:
ROBOCOPY :: Robust File Copy for Windows
Started : 24 July 2022 11:04:51 PM
Source : D:\PowerShellScripts\
Dest : F:\D_LAPTOP_BACKUP_2022-07-24\
Files : *.*
Exc Dirs : D:\System Volume Information
Options : *.* /TEE /S /E /COPYALL /PURGE /MIR /MT:32 /R:3 /W:3