1.10.2014

Powershell - Transferring Files from Server to Server

This is a nice little trick to get powershell to transfer files from one server directly without using domain admin credentials, or setting up an intermediary share directory. The credentials here are for the destination server. This creates a temporary network shared drive on the source server, transfers the file, then removes the shared directory.

$DriveUsername = "SERVER\USERNAME"
$DrivePassword = "PASSWORD"
$LocalFile = "C:\DIRECTORY\FILENAME.csv"
$Drive = "T:"
$RemoteFile = "$Drive\FILENAME.csv"
$UNC = "\\10.10.10.10\C$\DIRECTORY"
$net = new-object -ComObject WScript.Network

$net.MapNetworkDrive($Drive, $UNC, $false, $DriveUsername, $DrivePassword)
Copy-Item $LocalFile $RemoteFile
$net.removenetworkdrive($Drive)