PowerShell MVP Oisin Grehan posted a very promising PowerShell module the other day. He calls it the PModem File Transfer Protocol. It is based on the old bulletin board file transfer protocols of the late 20th century, which I have to admit I fondly remember using. Of course Oisin’s work intrigued me and after playing with it for a while I realized I needed something else.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Oisin’s approach is to transfer files in PowerShell using the WinRM connection which is ubiquitous with PowerShell v2.0. I won’t go into the technical nitty gritty, but basically files can be transferred without using any of the traditional file transfer protocols. What I realized I needed was a way to see what files are on the remote computer. I could do a simple listing
PS C:\> dir \\remotepc\c$\mydata\test
But then I’m back to using the old-fashioned file protocols. It took me a few minutes to realize how to get a remote listing using WinRM: The Invoke-Command cmdlet. With this cmdlet I can run the DIR command on the remote computer through a WinRM session. First I need to define a session.
PS C:\> $rdc=new-pssession -ComputerName ResearchDC -credential $research
Then I can use Invoke-Command to run my remote directory listing.
PS C:\> invoke-command $rdc -ScriptBlock {dir $env:temp} -hide
Directory: C:\Users\Administrator\AppData\Local\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 8/25/2009 2:40 PM bin
d---- 8/18/2009 8:18 PM Low
d---- 8/18/2009 2:20 PM VMwareDnD
-a--- 10/23/2009 1:19 PM 123 CFG5F0C.tmp
-a--- 10/23/2009 1:23 PM 123 CFG839D.tmp
-a--- 8/25/2009 2:41 PM 2241856 msi.log
-a--- 9/16/2009 10:05 AM 1648 Silverlight0.log
-a--- 9/16/2009 10:05 AM 604370 SilverlightMSI.log
-a--- 10/13/2009 4:21 PM 54558 vminst.log
I’m hiding the remote computername because I already know what it is. Now that I know the file name I want I can use the Get-RemoteFile function.
PS C:\> get-remotefile -session $rdc -remotefile C:\Users\Administrator\AppData\Local\Temp\msi.log -local c:\test
This is very early code so there’s alot of kinks to work out but this works! Even better, I can take my expression and pipe it to Get-Remotefile and retrieve all the log files.
invoke-command $rdc -ScriptBlock {dir $env:temp\*.log} -hide |
foreach {
get-remotefile -session $rdc -remotefile $_.fullname -local c:\test
}
I know Oisin wants to improve multiple file transfers but for now this works for me and I know it will only get better. Thanks Oisin.