2

As part of project requirement, I am preparing a script to copy the files from local computer to remote servers ( with username and password )

I have tried with below ways for files are 27 KB and 50 MB size

i. Using ReadallBytes and WriteAllBytes this is working for small file 27 KB, where as for 50 MB its taking 100% CPU and taking too much of time

$myfile = [System.IO.File]::ReadAllBytes("C:\Temp\test\a.txt")
$Stat = $null
$session=$null
$session = New-PSSession -computerName $server -credential $user
$Stat = Invoke-Command -Session $session -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes("C:\temp\a.txt", $args)} -ErrorAction Stop

ii. I tried to copy with Copy-Item , but issue is target directory is not mount pointed

$Stat = Invoke-Command -ComputerName $server -ScriptBlock { Copy-Item -Path "C:\Temp\test\a.txt" -Destination "C:\temp\a.txt" -Recurse -Force -PassThru -Verbose } -Credential $user

Struck in both ways, please suggest any other way to achieve without mounting the target folder

San
  • 226
  • 5
  • 14
  • Consider PowerShell's built in file transfer commands: `Copy-Item` or `Start-BitsTransfer`. If you really need speed / find yourself dealing with much larger files, consider third party solutions such as TeraCopy (http://www.codesector.com/teracopy); though in the scenario you mention, this wouldn't be required. – JohnLBevan Aug 14 '17 at 09:01

2 Answers2

3
Copy-Item -Path "C:\Temp\test\a.txt" -Dest "\\$($server)\c$\temp\a.txt"

use the built-in drive shares to copy it over, you may need to provide creds for this.

you might find this helper function useful to get the remote path correctly.

Function Get-RemotePath($Server,$Path){
    "\\$($Server)\$($Path -replace ':','$')"
}

Get-RemotePath -Server "SERVER01" -Path "C:\Temp\File.txt"

\\SERVER01\C$\Temp\File.txt
colsw
  • 3,216
  • 1
  • 14
  • 28
1

Why not use WMI top copy the file instead?

It can be asynchronous and is very efficient.

I have a post here which explains it.

Powershell - Copying File to Remote Host and Executing Install exe using WMI

John Foll
  • 121
  • 1
  • 10