Problem: Robocopy not launching as another user in Start-Process
The script works fine when running on an account that has the permissions for both file locations but it just doesnt seem to be accepting the -credential
param.
Unsure if my formatting is incorrect or if I am doing something wrong.
# Create Password for credential
$passw = convertto-securestring "Password" -asplaintext –force
# Assembles password into a credential
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username", $passw
# Select a source / destination path, can contain spaces
$Source = '\\Source\E$\Location'
$Destination = '\\Destination\Location Here'
# formats the arguments to allow the credentials to be wrapped into the command
$RoboArgs = "`"$($Source)`" `"$($Destination)`"" + " /e /Copy:DAT"
# Started Robocopy with arguments and credentials
Start-Process -credential $creds Robocopy.exe -ArgumentList $RoboArgs -Wait
Robocopy.exe does not provide network authentication by itself. So to provide username and password for robocopy.exe we can use NET USE to open IPC$ share to destination host and execute our robocopy code. Note: There is UNC path used, so keep in mind to have source and destination folders shared.
One way is to first mount the drives/folders from the source and destination servers (this will require the credentials), then run the robocopy command. For example: net use \\SourceServer\D$ /user:Domain1\Admin1 * net use \\DestinationServer\D$ /user:Domain2\Admin2 *
Copy examples The easiest way to copy a folder with all files and subfolders is to run this command: robocopy c:\temp\source c:\temp\destination /E /DCOPY:DAT /R:10 /W:3. The /E switch tells Robocopy to copy all subfolders, including empty ones. If you don't want to copy empty subfolders, use the /S switch.
Robocopy will use the standard windows authentication mechanism.
So you probably need to connect to the servers using the appropriate credentials before you issue the robocopy command.
You can use net use
to do this.
net use X: '\\Source\E$\Location' /user:MYDOMAIN\USER THEPASSWORD
net use Y: '\\Destination\Location Here' /user:MYDOMAIN\USER THEPASSWORD
net use X: /d
net use Y: /d
and then start your ROBOCOPY
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With