AzuraCF
AzuraCF

Reputation: 25

use csv to copy and paste files from remote servers

I want a csv that has two columns, it will look like this. I need a script that'll copy a file from the old server and paste it in my local directory then from my local directory to the new server.

Old_Server New_Server
OServer1 Nserver939
Oserver2 NServer32

Right now I only have a csv with one column and it has the old server names. The csv is in the temp so I have

$servers = Get-Content -Path "C:\Temp\Servers.csv"

temp.csv

Old_Server
OServer1
Oserver2

I have the code that copies the file from the remote server to my local one.

forEach ($server in $servers){
copy -path "\\$server\D$\topsecret\secret.txt" -destination "C:\Temp\$server\TopSecret\"
} 

I need to use the old servername to copy the file inside it from my local directory and paste it into the new server's location.

Upvotes: 1

Views: 164

Answers (1)

mklement0
mklement0

Reputation: 438008

Untested, using New-PSSession (and Remove-PSSession) in combination with Copy-Item's -ToSession parameter:

Import-Csv C:\Temp\Servers.csv |
  ForEach-Object {
    $toSession = New-PSSession $_.NewServer
    Copy-Item "\\$($_.OldServer)\D$\topsecret\secret.txt" `
              -Destination 'C:\some\local\path\on\the\new\server'
    $toSession | Remove-PSSession
  }

Of course, if you can target the target directory on the new server via a UNC path as well, you don't need to create an explicit session, and can use something like
-Destination "\\$($_.NewServer)\D$\topsecret\secret.txt"

Upvotes: 1

Related Questions