Robocopy作为另一个用户

时间:2018-08-22 04:56:42

标签: powershell robocopy

问题:Robocopy无法在Start-Process中以其他用户身份启动

在具有两个文件位置权限的帐户上运行时,脚本运行良好,但似乎并没有接受-credential参数。

不确定我的格式是否正确或我做错了什么。

# 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

2 个答案:

答案 0 :(得分:4)

Robocopy将使用标准的Windows身份验证机制。

因此,在发出robocopy命令之前,您可能需要使用适当的凭据连接到服务器。

您可以使用net use来完成此操作。

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

,然后启动您的ROBOCOPY

答案 1 :(得分:1)

S.Spieker的答案会起作用,但是如果您想使用PowerShell内置命令并将凭据作为凭据对象传递,则可以使用New-PSDrive来安装驱动器:

    $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 

*我可能把Robocopy弄错了,使用它已经有好几年了,但是安装驱动器是正确的。