PowerShell- copy item cannot bind argument to parameter 'path' because it is null

SQLLover21 201 Reputation points
2020-12-02T17:24:13.423+00:00

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.

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

Here is the result output- showing that the path is null.

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?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,388 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,557 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,376 Reputation points Microsoft Vendor
    2020-12-03T06:12:37.47+00:00

    Hi,

    You see the error message because $LatestBackupFileName is null. If Sample_full.bak is a file, the first line should be

    $BackupDir = "C:\SQL Installs\Backups\Full Backups"  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 97,486 Reputation points MVP
    2020-12-02T17:56:57.173+00:00

    This looks suspicious:

    $BackupDir = "C:\SQL Installs\Backups\Full Backups\Sample_full.bak
    

    Is Sample_full.bak a file? Or it's a subfolder?

    What is the content of the variable $LatestBackupFileName if you run the script?


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 45,096 Reputation points
    2020-12-02T19:02:48.697+00:00

    I'm pretty sure the Get-ChildItem is incorrect. I think you're looking for all the .bak files in $BackupDir? If so, try this:

    $LatestBackupFileName = (Get-ChildItem $BackupDir -Include *.bak | sort LastWriteTime | select -last 1)

    0 comments No comments