在使用Robocopy的Powershell脚本中需要帮助

时间:2019-12-18 07:27:12

标签: powershell-remoting robocopy

我想从服务器A运行Power Shell脚本,该服务器连接到服务器B,并将文件从服务器B复制到服务器C。

如何解决下面编写的代码中的错误?

$username = 'xyz'
$password = 'abc@'
$pass = ConvertTo-SecureString -AsPlainText $password -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username,$pass
$session_source = New-PSSession -ComputerName <server B> -Credential $cred
$session_target = New-PSSession -ComputerName <Server C> -Credential $cred
$source_file = Invoke-Command -Session $session_source -ScriptBlock{"D:\temp\abcxyz.bkup"}

$Space       = Write-host "" 
$Dest_PC     = "server C" 
$dest        = "D:\temp\abcxyz.bkup"
$date        = Get-Date -UFormat "%Y%m%d" 

## Get Start Time 
$startDTM = (Get-Date) 

# The below code is to open powershell as an Administrator from remote
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

## Kick off the copy with options defined  
Invoke-Command -Session $session_target -ScriptBlock{ROBOCOPY $source_file $dest /COPYALL /B /SEC /R:0 /W:0 /NFL /NDL}

ERROR:
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Tuesday, December 17, 2019 11:08:32 PM
   Source - 
     Dest - 

    Files : *.*

  Options : *.* /NDL /NFL /DCOPY:DA /COPY:DATS /B /R:0 /W:0 

------------------------------------------------------------------------------

ERROR : No Source Directory Specified.

       Simple Usage :: ROBOCOPY source destination /MIR

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               /MIR :: Mirror a complete directory tree.

    For more usage information run ROBOCOPY /?

****  /MIR can DELETE files as well as copy them !

0 个答案:

没有答案