jeffharbert

Enumerate and backup profiles from remote PCs using Robocopy

Mar 30th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##  Enumerate and backup profiles from remote Windows PCs using Robocopy
  2. ##  Written by Jeff Harbert 3-30-2017
  3. ##  This Powershell script is intended to be run once per day as a scheduled task
  4.  
  5. #   Backups will go into folders named for days of the week.
  6. $day = (get-date).dayofweek
  7.  
  8. #   List of computers to get profiles from.
  9. $computers = @("computer1","computer2","computer3")
  10. foreach ($computerName in $computers)
  11. {
  12. #   List of profiles will be saved to this file.
  13. $saveFile = $computerName + "_profiles.txt"
  14.  
  15. #   Grab the profiles form the remote PC and save the list to the file above.
  16. gwmi -Namespace "root\cimv2" -Class win32_userprofile -Impersonation 3 -ComputerName $computerName | select LocalPath > $saveFile
  17.  
  18. #   Start processing the file to isolate the user profile names.
  19. $lines = (gc $saveFile)
  20. foreach ($line in $lines)
  21. {
  22.     $b = $line.contains("Users")
  23.     If ($b -eq "True")
  24.         {
  25.         $array = $line.Split("\")
  26.         $userName = $array[2]
  27.         #   The output of the command above results in trailing whitespace. The next command eliminates it.
  28.         $userName = $userName.Trim()
  29.         #   Specify the folders to be backed up
  30.         $folders = @("Desktop","Downloads","Documents","Music","Pictures")
  31.         foreach ($folder in $folders)
  32.             {
  33.             #   This creates the Robocopy command that will be run for each folder listed above.
  34.             $cmd = '& robocopy /e /xo /nfl /r:5 /w:2 \\' + $computerName + '\c$\Users\' + $userName + '\' + $folder + ' f:\profiles\' + $day + '\' + $computerName + '\' + $userName + '\' + $folder + '\'
  35.             #   Execute the Robocopy command.
  36.             Invoke-Expression $cmd
  37.             }
  38.         }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment