0

So, to make it simple, i'm trying to copy user profiles through a shared folder on multiple clients, it works fine, but i can't seem to make a log file that works with it

As it is, i expected that $? would always end up giving me True for each command (Unless it kinda crashes in the middle of copying), since it's properly copying every folder i want, but it's not... It gives me a True once, then it's all false, even though it did everything correctly

    Foreach ($User in $Users){
        Foreach ($TargetDirectory in $Directories){            
            $TestPath = Test-Path -Path "\\$Client\Users\$User"
            If ($TestPath -eq $True) {   
                            
                Robocopy "\\$Client\Users\$User\$TargetDirectory" "C:\SAV\$Client\$User\$TargetDirectory" /E /MIR
                
                $Date = get-date -f yyyy-MM-dd
                $Path = "C:\Logs\($Date)Sauvegarde_Profils_$Client.txt"
                New-Item -Path $Path -Force
                $Result = "Copie du dossier $TargetDirectory, appartenant à $User = $? dans $Client"
                Add-Content -Path $Path $Result
            }
        }
    }
}

Here's a sample of the logfile (in French, but it's just to show that it's almost working as intended) :

Copie du dossier Documents, appartenant à firmerie.alain = True pour CLIENT-1

Copie du dossier Desktop, appartenant à firmerie.alain = False pour CLIENT-1

...(Only False after that point)

  • Isn't it kind of odd that `$User` would contain `firmerie.alain = False pour CLIENT-1`? `$Client` is never defined. Did you have a look at the actual robocopy output? Maybe it is not sucessfull? – Seth Aug 27 '21 at 11:09
  • I didn't include the full code because it'd be quite long, but all the variables are fine by themselves, robocopy works flawlessly It's just that part about the log i can't manage to make work, and said part about the log is reusing those variables, hence why the part about the log is almost "fine" It's just that the $? does seem to only work for a single iteration of robocopy (since it does every combination, for each client, user and folder, then it just returns a false value, while the copy really functioned ... – Crysos Is back Aug 27 '21 at 12:14
  • Which command run are you trying to capture by `$?`? Maybe you need to get `$LastExitCode` into a variable straight after `robocopy` execution, otherwise robocopy is not the last command that you have executed. – andr3yk Aug 27 '21 at 22:24
  • Yeah that's robocopy i'm interested in having in $?, but it doesn't seem to properly work for whatever reason Also, as quite the newbie, i'm going to look into $LastExitCode, but i've never seen that so far, we'll see how that goes (don't hesitate if you've got examples of how it behaves) – Crysos Is back Aug 29 '21 at 02:05

1 Answers1

0

See here for the difference between $? and $LASTEXITCODE, the official documentation would be in about_Automatic_Variables. Have a look at the description for $? as scoping can be important.

In your example $? Would contain the result for New-Item. This should however succeed as you have provided the -Force switch. Naturally you could opt to run this only once. Doing this might change the behavior slightly if it's a long running script. As you will only set the date initially and not per directory. So if you happen to copy directory A today and directory B tomorrow they will use the same logfile. With your current design they would be separate.

With your current design your log file path is set for every directory of the same user but your log file only ever references the user. So you could pull that whole check upwards. You should consider whenever you want to use $LASTEXITCODE instead. You might also want to include a check for $TargetDirectory.

Foreach ($User in $Users){
    $date = get-date -f yyyy-MM-dd
    $logfile = "C:\Logs\($Date)Sauvegarde_Profils_$Client.txt"
    $path = "\\$Client\Users\$User"
    
    if( (Test-Path -Path $logfile) -eq $false){
        New-Item -Path $logfile
    }

    if( (Test-Path -Path $path) -eq $false){
        Write-Error "Path for $user on $client does not exist."
        continue
    }
    
    Foreach ($TargetDirectory in $Directories){            
        Robocopy "\\$Client\Users\$User\$TargetDirectory" "C:\SAV\$Client\$User\$TargetDirectory" /E /MIR
        $Result = "Copie du dossier $TargetDirectory, appartenant à $User = $? dans $Client"
        Add-Content -Path $Path $Result
    }
}
Seth
  • 1,215
  • 15
  • 35