Robocopy作为另一个用户

问题描述 投票:2回答:2

问题: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
powershell robocopy
2个回答
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
投票

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,我使用它已经好几年了,但是安装驱动器是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.