Robocopy 作为另一个用户

Robocopy as another user

问题: 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

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

S.Spieker 的答案有效,但如果您想使用 PowerShell 内置命令并将凭据作为 pscredential 对象传递,您可以使用 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 弄错了,我已经好几年没用过了,但安装驱动器是正确的。