0

I have some issue with Sync center on my computer since I upgrade it to W10, so I try to write an alternative script that compare the sync folders/files on a desktop and our ServerFiles and copy the latest version of each folders/files on the user computer.

Thanks to the community I found some code, but it's doesnt work well for me

Help1 Help2

I'm a newbie on scripting with powershell 5.1. Im working with a WS2008 and the desktop on W10.

$SourceFolder = Get-ChildItem -Recurse "\\Srvfiles\USERS\user1\"
#"user1" contains 2 folders and each one have subfolders/files 

$TargetFolder = Get-ChildItem "D:\Doc_Sync"

$Result = Compare-Object $SourceFolder $TargetFolder -Property Name, Length 

foreach ($folder in $Result) {
        if ($folder.SideIndicator -eq "<=") {
            $FullSourceObject = $folder.InputObject.FullName
            $FullTargetObject = $folder.InputObject.FullName.Replace($SourceFolder, $TargetFolder)

            Copy-Item "\\Srvfiles\USERS\user1\$($_.name)" -Destination $Get-ChildItem "D:\Doc_Sync" -Recurse -Force
            }
        }

The good new is it working for somes folders, on D:/Doc_Sync it paste most of the files but(bad new), for the rest of them , I got many error :

Cannot bind argument to parameter « DifferenceObject » because it is null.
At line:5 char:40
+ $Result = Compare-Object $SourceFolder $TargetFolder -Property Name,  ...
+                                        ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData : (:) [Compare-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

You cannot call a method on a null-valued expression.
At line:10 char:13
+             $FullTargetObject = $folder.InputObject.FullName.Replace( ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Thanks for your futur help

  • the errormessage is pretty clear! your variable $Targetfolder and $folder are null (empty) – guiwhatsthat May 23 '19 at 11:27
  • @guiwhatsthat Thanks for your quick answer. If $Targetfolder and $folder are empty, why is it copying some files and some not ? –  May 23 '19 at 12:04
  • Not the folder variable is empty, it does not contain a property called InputObject. An d the other variable can be empty if the folder which you use is empty. – guiwhatsthat May 23 '19 at 12:46
  • Use breakpoints to debug your script, so you will see what exactly is in your variables – guiwhatsthat May 23 '19 at 12:47

1 Answers1

0

I found what's wrong with the first error, thanks to the breakpoints. I changed the $TargetFolder like this :

$TargetFolder = @(Get-ChildItem -Recurse "D:\Doc_Sync")

But I still have the second error with the "$FullTargetObject". Idk what's wrong with that line... I'll work on it this weekend.