Search code examples
vb.netrobocopy

How to run Robocopy programmatically elevated from vb.net and preserve security settings


I am attempting to run robocopy from a vb.net application to copy many folders & files and preserve timestamps, ownership and security of files. While testing, a command such as:

robocopy sourcefolder destfolder /COPYALL /E /DCOPY:T /SECFIX

succeeds when run from a command line that has been launched elevated with a right-click of RunAs Administrator.

I then attempted to run the command in various methods, including

Dim psi As New ProcessStartInfo
With psi
  .Verb = "runas"
  .UseShellExecute = False
  .UserName = "myadminname"
  .Domain = "mydomainname"
  .PasswordInClearText = "mypassword" 'For testing only
  .FileName = Command
  .Arguments = Args
End With
Process.Start(psi)     

I am under the impression that the 'runas' verb elevates the command the same as if run from a 'runas' administrator commandline. However, robocopy fails with the same error as when run from a non-elevated command line prompt, i.e. "You do not have the Manage Auditing user right. You need this to copy auditing information."

My user/password (used to log into the windows machine) can perform the operation from the command line with runas admin, but the app

Is there another reason that robocopy is not getting the permissions it requires?


Solution

  • UseShellExecute needs to be True for Process.Start to execute anything elevated to administrator. It may be that it needs to be True for verbs to work at all.