0

I have a case where a FileServer migration was not successful because the user who did not have permission on all folders.

So I have two servers with the same folder structure, but as users have already worked on the folders I can't copy EVERYTHING again, as it could take files that the user has already deleted, for example.

However, I noticed a pattern, folders not copied due to permission error were left empty in the destination.

So, I found it by searching the following powershell script, which lists all empty folders at destination:

(gci E:\Departments\Marketing -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName

What I have been doing is accessing folders, for example in E:\Departments\Marketing\ and copying from \oldserver\e$\departments\Marketing\

But there are a LOT of folders to do manually.

Does anyone know a script to nest this with a foreach to get each folder and copy the data from the UNC path and copy it to the new one?

1
  • your question is confusing, can you rephrase a bit Commented Aug 11, 2021 at 21:16

2 Answers 2

0

I honestly still don't fully understand what you mean, the phrasing just throws me off. If you have an older server with the same file structure as the new server, you could just perform a simple copy again.

  • Using Copy-Item you can copy it from, a UNC path, to a local path, or from a session, to a local path, or from a local remote path (remote server) to a local path on your system.
  • In your case, to keep it simple, you can use the last option seeing it's the same folder structure and copying from sessions is a lot faster than through UNC.
  • If there are items already copied over, unless you specify the -Force switch, the already there matching items will not be copied over, but will be skipped.
$CopyVal = @{
    Path        = 'E:\departments\Marketing\'
    Destination = 'E:\departments\Marketing\'
    Recurse     = $true
    ErrorAction = 'SilentlyContinue'
}
$PSSession = New-PSSession -ComputerName OldServer
Copy-Item -FromSession $PSSession @CopyVal
3
  • I only need to copy data into folders that are empty at destination. Folders that already have files have been successfully copied and users have already worked on them. If I copy everything again, I can take back files they've already deleted, for example.
    – Lekost93
    Commented Aug 11, 2021 at 21:51
  • Does my 3rd bullet not answer your question? Commented Aug 11, 2021 at 22:29
  • unfortunately no, my problem is not already existing items but that users may have deleted files and i will send them back. I only need to copy folders that exist at the source and are empty at the destination, as they are definitely not copied due to a permission error.
    – Lekost93
    Commented Aug 12, 2021 at 14:16
0

Instead of piping the result to a Select statement, pipe it to a ForEach, create a source path from the destination path ($_.FullName), and use that result in an XCopy command, like this:

(gci "E:\Departments\Marketing" -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | ForEach {
    $Source = ($_.FullName -Replace 'E:\\Departments\Marketing','\\ServerName\SomeDir')
    XCopy $Source $_.FullName /s
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .