2

What I am trying to do is to backup a user profile from their local workstation to our backup servers and send me an email once it's complete. I currently have this is two different scripts. It would be nice if we could make this in one script. If I need two scripts, that won't be a problem.

The first script is the backup, and it has been working just fine.

robocopy C:\Users\TravisWhiteman.ArchwaySys\AppData \\10.1.10.6\WorkstationBackup\Test\AppData /mir /W:3 /R:1 /log:CopylogAppData.txt
robocopy C:\Users\TravisWhiteman.ArchwaySys\Desktop \\10.1.10.6\WorkstationBackup\Test\Desktop /mir /W:3 /R:1 /log:CopylogDesktop.txt
robocopy C:\Users\TravisWhiteman.ArchwaySys\Documents \\10.1.10.6\WorkstationBackup\Test\Documents /mir /W:3 /R:1 /log:CopylogDocuments.txt
robocopy C:\Users\TravisWhiteman.ArchwaySys\Downloads \\10.1.10.6\WorkstationBackup\Test\Downloads /mir /W:3 /R:1 /log:CopylogDownloads.txt

Now I want to add in a few features, and I don't know how. I want to change it from manually setting the user profile directory to the system automatically find out who the user is. I think it's something like %USERNAME%. The goal is having the system figure the user out is so I don't have to change the C:\Users\TravisWhiteman.ArchwaySys for every workstation. All of our workstations turns on automatically, 10 min before the scheduled task to backup, in case a user were to shut off their computer.

sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • 1
    Hi, I think you should split this into two separate questions. In the one about the attachment, include the actual code that fails. About the other one, define which user's profile should be used (currently logged on? last logged on?). – sodawillow Jan 04 '17 at 18:29
  • @sodawillow Separating into two different posts based on advise. – Travis Whiteman Jan 04 '17 at 19:42
  • All of our employees stay signed in at the end of the day. After 10 min, the computer locks and will force them to re-sign in. I'm thinking the one I would need is the currently logged on. – Travis Whiteman Jan 04 '17 at 19:43

1 Answers1

1

Basically, what you need is the profile path of the currently logged on user for a list of remote computers.

Steps for each computer:

  1. Get the currently logged on user's login name (here is the method I currently use)
  2. Get the SID for this user - let's say $userSID (a method is described here)
  3. Browse this registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$userSID on the remote computer, and read the value ProfileImagePath, it holds the local profile path for this user on this computer (example of remote registry access)
  4. Convert the local path to a network path (C:\Users\... -> \\computerName\c$\Users)
  5. Call robocopy and get some coffee (removed coffee from loop)

One could simply go for \\computer\c$\Users\$userLogin but as OP's example demonstrates it, Windows sometimes appends your domain name to your user name in your local profile folder name, in quite an unpredictable fashion.

(the Remote Registry service must be running on the remote computers)

If the workstation was shut down and then awoken, you I'd target the last modified folder in C:\Users.

Community
  • 1
  • 1
sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • 1
    Once I enabled the Remote Registry Service on all of our workstations, the script worked fine. When you said in step 4, of changing from a local path to a network, I played around with that and found I could consolidate our scripts down. So instead of having one script on every computer, I am going to run one script from one computer. – Travis Whiteman Jan 04 '17 at 22:14
  • 1
    Thanks again @sodawillow. I was running through this project faster than ever. :) – Travis Whiteman Jan 05 '17 at 22:45