问题
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
回答1:
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
回答2:
S.Spieker's answer will work, but if you want to use PowerShell built in command and pass the credentials as a pscredential object you could use New-PSDrive to mount the drives:
$passw = convertto-securestring "Password" -asplaintext –force
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username", $passw
$SourceFolder = '\\Source\E$\Location'
$DestinationFolder = '\\Destination\Location Here'
New-PSDrive -Name MountedSource -PSProvider FileSystem -Root $SourceFolder -Credential $creds
New-PSDrive -Name MountedDestination -PSProvider FileSystem -Root $DestinationFolder -Credentials $creds
Robocopy.exe \\MountedSource \\MountedDestination /e /Copy:DAT"
Remove-PSDrive -Name MountedSource
Remove-PSDrive -Name MountedDestination
* I might have the Robocopy wrong, it's been years since I used it, but the mounting drives is correct.
来源:https://stackoverflow.com/questions/51960100/robocopy-as-another-user