#建议保存编码为:bom头 + utf8

param
(
	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[String]$Path,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[String]$Destination,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("ipaddress")][String]$被控机ip地址,

	[Switch]$Recurse,

	[Switch]$Delete,

	[Alias("port")][ValidateRange(1,65535)][int]$端口 = 22,

	[Switch]$debugging
)

$msg = @'
注意:一般来讲,
源目录末尾,不带斜线,或反斜线。
目标目录末尾,应该带斜线。

用法:把win主控机的,'e:\temp7'目录,含子目录。同步到linux被控机的'/tmp/',目录内。
.\k_rsync_wintolinux.ps1 -Path 'e:\temp7' -Destination '/tmp/'  -被控机ip地址 '192.168.1.2' -Recurse -Delete -debugging
后三个参数可选。

'@
Write-Warning $msg

if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
	& 'c:\ProgramData\kasini3000\0k_source.ps1'
}

if ($IsLinux -eq $True)
{
	Write-Error '错误:不支持linux系统。返回码1'
	exit 1
}

if (Test-Connection -TargetName $被控机ip地址 -TcpPort $端口 -Quiet)
{
}
else
{
	Write-Error "错误,目标ip【${被控机ip地址}】的【${端口}】端口不通"
	exit 2
}

#main
if ($Recurse)
{
	$Recurse2 = '--recursive'
}

if ($Delete)
{
	$Delete2 = '--delete'
}

if ($debugging)
{
	$debug2 = '-vvv'
}

$path2 = '/cygdrive/' + $path.Replace(':','').Replace('\','/')

$Private:ssh_cmd = @"
c:\ProgramData\kasini3000\lib\cwrsync\ssh.exe -o StrictHostKeyChecking=no -i ${env:USERPROFILE}\.ssh\id_rsa
"@

$Private:rsync_cmd = @"
c:\ProgramData\kasini3000\lib\cwrsync\rsync.exe -e '$Private:ssh_cmd' $debug2 $Recurse2 $Delete2 --progress $path2 root@${被控机ip地址}:${Destination}
"@
Write-Verbose $Private:rsync_cmd

if ($debugging)
{
	Write-Warning $Private:rsync_cmd
	Pause
}

Write-Verbose '从win主控机,用rsync,复制文件到linux被控机,开始 ...'
Invoke-Expression -Command $Private:rsync_cmd
Write-Verbose '从win主控机,用rsync,复制文件到linux被控机,结束 ...'

exit 0