Remotely Copy Files To Multiple Destinations Using PowerShell
Basic Copy-Item cmdlet
If you have used some PowerShell, you are probably familiar with Copy-Item command (cmdlet) to copy files, among other things. Here is a simple example to copy a file on local machine from one folder to another:
Besides the files and folders, you can also use to copy other types of objects, depending on whether the object provider supports the Copy-Item. Here is an example of copying registry keys.
Similarly, you can also use the cmdlet to copy certificates. However, PowerShell provider for SQL Server or SQLPS don't support Copy-Item, even though it may support other *Item cmdlets.
You can find out more about the Copy-Item straight from the horse's mouth at Copy-Item.
Copying files between remote computers
There are occasions when a DBA needs to copy file/s from their local computer to a SQL Server, or between two SQL Servers. Reasons aside, there are few ways you can copy the files, including PowerShell. For example:
That's looks simple enough. However, the destination parameter only accepts one value so you cannot use it to copy files to multiple destinations. Plus you are using your local computer as a conduit so its not the most efficient either.
PowerShell Remoting
Remoting is a feature of PowerShell that allows you to run commands on remote computer/s. If you are on a Windows 2012 or higher, the PowerShell remoting is enabled by default. Otherwise, you may need to enable it first:
Start the PowerShell session with elevated privileges (Run As Administrator).
Enable-PSRemoting –Verbose
You may be familiar with the Invoke-Command, which maybe the most typical command used to run commands on remote computer/s. You can store results returned by it into a local variable for further processing.
Invoke-Command -ComputerName SQLVM01 -ScriptBlock {"Hello World"}
# Or store the results into a local variable$local_variable = Invoke-Command -ComputerName SQLVM01 -ScriptBlock {"Hello World"} Write-Host$local_variable -ForegroundColor White -BackgroundColor Blue
But, Invoke-Command doesn't persist the connection. In order to work with remote computers using persistent connections, PowerShell provides cmdlets whose names end with PSSession:
But those are the not only commands that supports the remoting feature. But there are several other PowerShell commands that have remoting built-in. For example Get-Process, Get-Service etc. and I consider Copy-Item to be one of them. One way you can find out what commands allows running it on a remote computer (or a list of computers in one go) is to check if they accept ComputerName as an input parameter.
Get-Command -ParameterName ComputerName
Alas, you won't find the Copy-Item in that list. So what the hell I am talking about? How can I use it to copy files remotely?
Among the parameters for Copy-Item command, there are two parameters FromSession and ToSession, which combined with the official PowerShell remoting commands *PSSession, can be used to copy files from/to remote computers.
Get-Help -Name Copy-Item -Parameter *Session
But here is the twist, based on parameters name, you would think you can use them together to copy files between two computers in one go like this:
You are probably thinking, well this isn't the most efficient method, whichever way you look at it. Plus most laptop or desktop computers don't have kind of disk space to hold large SQL database files so they cannot be used to first copy large files to local computer and then copy it to another computer. Besides, the ToSession parameter only accepts a single value, so can't be used to copy files to multiple computers in one go.
In this second method. You can send the Copy-Item command, as a script block, to one or more destination computers to copy file/s from the source computer. Advantage of this is that you are not using your local computer as a conduit so it's more efficient and you can copy the file/s to multiple destinations simultaneously.
Here I am sending the command to the target servers to get the file from the source server:
However, there is still one more tweak needed. Because we are sending the command to another server to run, which then opens a remote session to another computer, that involves a situation called double-hop. In SQL Server and some other applications like IIS, you can configure it to seamlessly impersonate user through Kerberos and SPNs i.e. without needing to prompt user to re-enter their credential. So far I am not aware PowerShell remoting supports it, yet.
So, you can either manually enter user credential into the script block, which can get really annoying and inconvenient because it will prompt for password for every destination server. Or, you can store your credentials into an encrypted credential object then pass it to the script block: