使用 PowerShell 的 Robocopy:错误 3 (0x00000003) 系统找不到指定的路径
Robocopy with PowerShell : ERROR 3 (0x00000003) The system cannot find the path specified
我有一个用于备份的 PowerShell 脚本,我在 Windows 2003 Server 上使用了很长时间,现在我在 Windows Server 2012 R2 上遇到了问题。当我尝试执行它时收到此消息:
2019/09/18 08:40:25 ERROR 3 (0x00000003) Creating Destination Directory Z:\BACKUP\D\ The system cannot find the path specified.
我 运行 PowerShell ISE 中的 PowerShell 脚本作为管理员或作为具有最高权限的计划任务。该脚本在具有源驱动器的域 (DOMAIN1) 上的服务器上执行,目标位于另一个域 (DOMAIN2) 的另一台服务器上。域之间没有信任。
当我使用 net use
命令时,它在 PowerShell ISE 上运行良好,但无法通过计划任务运行。当我使用 New-PSDrive
时,它在任何地方都不起作用。我插入 GCI
语句以确保驱动器已连接并列出目标目录的内容。
我简化了调试代码:
$strNetDrvPath = '\server.domain2.local\BkpDrv'
$strNetDrvUser = 'DOMAIN2\BkpUser'
$strNetDrvPwd = 'ABCD1234'
#net use Z: $strNetDrvPath /user:$strNetDrvUser $strNetDrvPwd
$objCredential = New-Object System.Management.Automation.PsCredential -ArgumentList $strNetDrvUser, ($strNetDrvPwd|ConvertTo-SecureString -AsPlainText -Force)
New-PSDrive –Name Z –PSProvider FileSystem –Root $strNetDrvPath -Credential $objCredential
gci Z: -Name
$strCmd = "robocopy D:\. Z:\BACKUP\D /B /MIR /NP /R:0 /LOG:D:\RC_Log.txt"
Invoke-Expression $strCmd
#cmd /c $strCmd
#net use Z: /delete /y
Remove-PSDrive Z
我用 net use
命令留下了评论来展示我的尝试。可能是什么问题呢?有没有其他可行的方法?
当您想使用New-PSDrive
映射Windows 网络驱动器时,您需要添加参数-Persist
。来自 documentation:
-Persist
Indicates that this cmdlet creates a Windows mapped network drive. Mapped network drives are saved in Windows on the local computer. They are persistent, not session-specific, and can be viewed and managed in File Explorer and other tools.
New-PSDrive –Name Z –PSProvider FileSystem –Root $strNetDrvPath -Persist -Credential $objCredential
由于您提供了明确的凭据,net use
应该可以在计划任务中正常工作。如果没有,则有其他问题(用于调试计划任务see here)。
此外,DO NOT USE Invoke-Expression
。 robocopy
可以直接从 PowerShell 运行。
$params = '/B', '/MIR', '/NP', '/R:0', '/LOG:D:\RC_Log.txt'
& robocopy D:\. Z:\BACKUP\D @params
我有一个用于备份的 PowerShell 脚本,我在 Windows 2003 Server 上使用了很长时间,现在我在 Windows Server 2012 R2 上遇到了问题。当我尝试执行它时收到此消息:
2019/09/18 08:40:25 ERROR 3 (0x00000003) Creating Destination Directory Z:\BACKUP\D\ The system cannot find the path specified.
我 运行 PowerShell ISE 中的 PowerShell 脚本作为管理员或作为具有最高权限的计划任务。该脚本在具有源驱动器的域 (DOMAIN1) 上的服务器上执行,目标位于另一个域 (DOMAIN2) 的另一台服务器上。域之间没有信任。
当我使用 net use
命令时,它在 PowerShell ISE 上运行良好,但无法通过计划任务运行。当我使用 New-PSDrive
时,它在任何地方都不起作用。我插入 GCI
语句以确保驱动器已连接并列出目标目录的内容。
我简化了调试代码:
$strNetDrvPath = '\server.domain2.local\BkpDrv'
$strNetDrvUser = 'DOMAIN2\BkpUser'
$strNetDrvPwd = 'ABCD1234'
#net use Z: $strNetDrvPath /user:$strNetDrvUser $strNetDrvPwd
$objCredential = New-Object System.Management.Automation.PsCredential -ArgumentList $strNetDrvUser, ($strNetDrvPwd|ConvertTo-SecureString -AsPlainText -Force)
New-PSDrive –Name Z –PSProvider FileSystem –Root $strNetDrvPath -Credential $objCredential
gci Z: -Name
$strCmd = "robocopy D:\. Z:\BACKUP\D /B /MIR /NP /R:0 /LOG:D:\RC_Log.txt"
Invoke-Expression $strCmd
#cmd /c $strCmd
#net use Z: /delete /y
Remove-PSDrive Z
我用 net use
命令留下了评论来展示我的尝试。可能是什么问题呢?有没有其他可行的方法?
当您想使用New-PSDrive
映射Windows 网络驱动器时,您需要添加参数-Persist
。来自 documentation:
-Persist
Indicates that this cmdlet creates a Windows mapped network drive. Mapped network drives are saved in Windows on the local computer. They are persistent, not session-specific, and can be viewed and managed in File Explorer and other tools.
New-PSDrive –Name Z –PSProvider FileSystem –Root $strNetDrvPath -Persist -Credential $objCredential
由于您提供了明确的凭据,net use
应该可以在计划任务中正常工作。如果没有,则有其他问题(用于调试计划任务see here)。
此外,DO NOT USE Invoke-Expression
。 robocopy
可以直接从 PowerShell 运行。
$params = '/B', '/MIR', '/NP', '/R:0', '/LOG:D:\RC_Log.txt'
& robocopy D:\. Z:\BACKUP\D @params