Invoking GCI on remote server to compare directories instead of slow UNC

This topic contains 6 replies, has 2 voices, and was last updated by  Neil Kronberg 1 year, 5 months ago.

  • Author
    Posts
  • #37378

    Neil Kronberg
    Participant

    Why doesn't this work as expected? Does it have anything to do with the extra PSComputerName property?

    I'm trying to compare the contents of 2 folders after a DFS replication that didn't quite complete as it should have, so I'm left with a source and a dest that are different. I need to detect the differences and copy the files/directories which were left behind. Oh, and there are over 160,000 files under this folder, so I need it to be quick. Hence the Invoke-Command, rather than simply running GCI against the UNC path. It's not over a super slow link, but it's not gigabit speed either, so efficiency is paramount when detecting differences.

    $source = Invoke-Command -computername remotecomputer -scriptblock {Get-ChildItem -Recurse -path "c:\users\user\desktop\folder"}
    $dest = Get-ChildItem -Recurse -path "c:\users\user\desktop\folder"
    Compare-Object -ReferenceObject $source -DifferenceObject $dest
    
  • #37388

    Richard Diphoorn
    Participant

    What exactly doesn't work? On my testlab the code works as you wrote it.

  • #37392

    Neil Kronberg
    Participant

    The code executes, yes, but despite both directories having identical file contents, Compare-Object returns 100% differences.

  • #37393

    Richard Diphoorn
    Participant

    And what if you try this? It the only will return the name of the files, and not the whole object for every file.

    $source = Invoke-Command -computername server01 -scriptblock {Get-ChildItem -Name -Recurse -path 'c:\temp'}
    $dest = Get-ChildItem -Name -Recurse -path 'c:\temp'
    Compare-Object -ReferenceObject $source -DifferenceObject $dest
    
  • #37394

    Neil Kronberg
    Participant

    I suppose I could do that, but then it wouldn't compare any other data, such as length. Nice short-term fix, though. I am wondering if anyone here can tell me exactly why this is working the way that it is – and if it's just a limitation of using Invoke-Command to run a Get-ChildItem and compare the differences with a locally run GCI.

  • #37395

    Richard Diphoorn
    Participant

    Compare-Object does exactly what the cmdlet name suggests; it compares object. It will compare ALL the properties of the objects that are being compared. A better way to use Compare-Object as opposed how I was suggesting, is actually specifying the property you want to compare on, like this:

    $source = Invoke-Command -computername server01 -scriptblock {Get-ChildItem -Recurse -path 'c:\temp'}
    $dest = Get-ChildItem -Recurse -path 'C:\Temp'
    Compare-Object -ReferenceObject $source -DifferenceObject $dest -Property PSChildname,LastWriteTime | Sort-Object PSChildName
    

    This will compare the filenames and last write time in both directories. Does this help you a bit?

  • #37397

    Neil Kronberg
    Participant

    Yeah, it does. Thanks! I guess that means if a Get-Member doesn't match up perfectly, Compare-Object will always show a left or right arrow indicator.

You must be logged in to reply to this topic.