Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to create a PowerShell script that will copy my backup files from PROD server to restore it on DEV server on a monthly basis. Then I need to create a SQL Agent job with two steps. The first step will be to restore those copied files onto DEV Server. The second step will be to execute a SP that cleans up the redundant/inconsistent data.


Here is the result output:

Set-Location $WorkDir
$LatestBackupFileName = (Get-ChildItem $BackupDir*.bak | sort LastWriteTime | select -last 1)
Copy-Item $LatestBackupFileName -Destination $WorkDir\AutomatedDbRefresh.bak -Force
Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:8 char:11
+ Copy-Item $LatestBackupFileName -Destination $WorkDir\AutomatedDbRefr ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand




Can someone please advise on how to avoid this error and what I may need to fix in my script?


What I have tried:

So far this is the script I created

begin set vars
$BackupDir = "C:\SQL Installs\Backups\Full Backups\Sample_full.bak" # where backups are stored
$WorkDir = "C:\SQL Installs\Backups\Test Backups" # where you are copying backup to
end set vars


Set-Location $WorkDir
$LatestBackupFileName = (Get-ChildItem $BackupDir*.bak | sort LastWriteTime | select -last 1)
Copy-Item $LatestBackupFileName -Destination $WorkDir\AutomatedDbRefresh.bak -Force
Posted
Comments
Richard MacCutchan 3-Dec-20 5:10am    
You need to find out why LatestBackupFileName is not getting set. Is C:\SQL Installs\Backups\Full Backups\Sample_full.bak really the name of the directory? It looks more like a filename.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900