Using the PowerShell
Copy-Item cmdlet, we can now copy files using PowerShell Remoting (WinRM). Here's how.
One of the cool features of the April 2015 WMF preview was a simple addition to the
Copy-Item cmdlet. What was once just a simple SMB copy,
Copy-Item now has two new parameters;
ToSession. �These two new parameters allow you to copy files over WinRM without the need for SMB.
At first I thought, "Why the big fuss? Just use SMB. It's faster". Then, I came across a problem in my consulting gig.
I needed to automate the deployment of VMs into a locked-down DMZ environment. The only port that was allowed to be opened was for WinRM. One small problem though. No server was running WMF v5 yet. But, I was lucky enough to come across Lee Holmes' old but still relevant function called
Send-File. Yay! This worked great sending a couple files over to the DMZ until I was faced with sending a folder full of files and folders. It needed to be reworked a bit.
This script is on Github is what I came up with. �The
Send-File function has now been modified to recursively send folders recursively over a remote session. �Also, Lee's didn't have support for UNC paths so I've added that as well. �Check it out and let me know what you think. �If you're used to the performance of SMB don't yell at me when it's slooow. However, when you're in a pinch it gets the job done.
Send-File function is easy. Simply create a new PSSession using
New-PSSession and provide that session to the
Session parameter on the
Send-File function along with the path of the folder or file to copy like so:
PS> $session = New-PSSession -ComputerName MYSERVER PS> Send-File -Path C:\test.txt -Destination C:\ -Session $session PS> $session | Remove-PSSession
Join the Jar Tippers on Patreon
It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!Become a Patron!