0

I have a script that copies the files from one server to another. I have basically 3 different server locations, that I want to copy from , and create on another server, a folder for each of the location from the source and the contents inside it. I made that, but I declared a variable for each source and each folder/destination. I want to create one variable, and from that it should automatically get each location of the source, and copy everything in the correct location. Would defining $server= "\path1 , \path2, \path3 " do it and it would go into a foreach loop? and go through each part of the path and copy and paste? If so, how can I define the destination if I have 1 folder with 3 subfolders each corresponding to one source. for example \path1 should alwasy put items in the path1destination , \path2 should always put items in the path2destination and so on. basically I want somehow to correlate that for each source path to have a specific path destination and everything should use as less variables as possible.

Anyone can provide and ideas on how to tackle this ? My code works but I had to define $path1 , $path2 , $path3 and so on, and then go for a loop on each, which is great but I need to make it clean and less lines of code .



$server1 = "C:\Users\nicolae.calimanu\Documents\B\"
$server2 = "C:\Users\nicolae.calimanu\Documents\A\"  # UNC Path. 
$datetime = Get-Date -Format "MMddyyyy-HHmmss"
$server3 = "C:\Users\nicolae.calimanu\Documents\C\" # UNC Path. 
foreach ($server1 in gci $server1  -recurse)
{ 
  Copy-Item -Path $server1.FullName -Destination $server2
}

ForEach ( $server2 in $server2 ) {
 $curDateTime = Get-Date -Format yyyyMMdd-HHmmss
 Get-ChildItem $server2  -Recurse | 
 Rename-Item -NewName {$_.Basename + '_' + $curDateTime + $_.Extension }
}

foreach ($server2 in gci $server2 -Recurse)
{ 
  Move-Item -path $server2 -destination "C:\Users\nicolae.calimanu\Documents\C"
}

3
  • please post your code in your Question ... and wrap it in code formatting markers. Commented May 14, 2020 at 7:04
  • this >>> foreach ($server1 in gci $server1 -recurse) <<< is very strange. you REALLY should not use the same variable name like that. also, what is in the $Var at that point? file objects? Commented May 14, 2020 at 7:44
  • In a foreach() loop, you do not have the benefit of the $_ automatic variable as you would if you use a ForEach-Object {} loop. In your case, $_.Basename does not work. Also, you keep using the same variable for both the iterating variable and the collection to iterate through. I have tried to explain that to you earlier in your previous question. Please read about looping with either foreach(..) and ForEach-Object{..} so you can see for yourself what you're doing wrong here. Commented May 14, 2020 at 8:31

1 Answer 1

1

Use a hashtable to create a key-value store for each source and destination. Like so,

# Create entries for each source and destination
$ht = @{}
$o = new-object PSObject -property @{ 
    from = "\\serverA\source"
    to = "\\serverB\destination" }
$ht.Add($o.from, $o) 

$o = new-object PSObject -property @{ 
    from = "\\serverC\source"
    to = "\\serverB\destination2" }
$ht.Add($o.from, $o) 

$o = new-object PSObject -property @{ 
    from = "\\servera\source2"
    to = "\\serverC\destination" }
$ht.Add($o.from, $o) 

# Iterate the collection. For demo, print the copy commands
foreach($server in $ht.keys) { $cmd = $("copy-item {0} {1}" -f $ht.Item($server).from, $ht.Item($server).to); $cmd }

# Sample output
copy-item \\serverA\source \\serverB\destination
copy-item \\servera\source2 \\serverC\destination
copy-item \\serverC\source \\serverB\destination2
Sign up to request clarification or add additional context in comments.

7 Comments

trying to create the copy, now . I thought of something like this ``` foreach($server in $ht.keys ) { Copy-Item $o.from -Destination $o.to } ``` I think this isn't quite right as I keep getting multiple errors.
ok, I got it to copy the folder. but it does not copy the contents, which is what would be best, I am not entirely sure why -recurse doesn't work on the foreach loop
@CălimanuLoredan Add -whatif to the copy command and study carefully the output. Without seeing your actual code and parameters, it's hard to say what might be the problem. Are you filtering the result set somehow?
I made the code but it's too long, I managed to get passed that, now Looking to another issue, still trying to figure it out, idk how to paste the code as it;s too long.
@CălimanuLoredan Create a new question and paste the code there. Comments are not intended for discussion and details.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.