如何在LastWriteTime上同步2个共享而不删除

时间:2019-05-16 13:22:55

标签: powershell powershell-v2.0 copy-item

我需要在不属于群集IIS的2个不同服务器后面同步2个FTP存储库。虚拟IP用于进行某种故障转移。

enter image description here

我尝试使用robocopy或xcopy需要具有参考目录。就我而言,当任务由第三台服务器启动时,我只需要复制上一个文件中的最后一个文件和目录即可。

$Global:pathFTP1 = $SRV1unc + "\c$\inetpub\PlcmSpIp"
$Global:pathFTP2 = $SRV2unc + "\c$\inetpub\PlcmSpIp"
$Global:Files1 = @()
$Global:Files2 = @()
$Global:tSRV = @(
    New-Object PSObject -Property @{Serveur = $SRV1; RacineFTP = $pathFTP1; Fichiers = $Files1}
    New-Object PSObject -Property @{Serveur = $SRV2; RacineFTP = $pathFTP2; Fichiers = $Files2}
)

这是代码的一部分:

$Global:Component = "Tree"
$Comparison = Compare-Object -ReferenceObject $tSRV.Item(0).Fichiers -DifferenceObject $tSRV.Item(1).Fichiers -PassThru -IncludeEqual
$ComparisonFolders = $Comparison | Where-Object{$_.PSIsContainer -eq $true}
$FoldersPresentOnEach = $ComparisonFolders | Where-Object {($_.SideIndicator -eq "==") -and (Test-Path($_.FullName))}
$FoldersNotPresentInSRV1 = $ComparisonFolders | Where-Object {($_.SideIndicator -eq "=>") -and (Test-Path($_.FullName))}
$FoldersNotPresentInSRV2 = $ComparisonFolders | Where-Object {($_.SideIndicator -eq "<=") -and (Test-Path($_.FullName))}
##Folders not present on $SRV1
if ($FoldersNotPresentInSRV1.Count -ne 0) {
    foreach ($Folder in $FoldersNotPresentInSRV1) {
        if (-not (Test-Path (($Folder.Fullname.Replace($SRV2unc,$SRV1unc)).Replace('\\\','\\')))) {
            New-Item -ItemType Directory -Path (($Folder.Fullname.Replace($SRV2unc,$SRV1unc)).Replace('\\\','\\')) -Force -ErrorAction SilentlyContinue
       }
    }
}
##Folders not present on $SRV2
if ($FoldersNotPresentInSRV2.Count -ne 0) {
    foreach ($Folder in $FoldersNotPresentInSRV2) {
        if (-not (Test-Path (($Folder.Fullname.Replace($SRV2unc,$SRV1unc)).Replace('\\\','\\')))) {
            New-Item  -ItemType Directory -Path (($Folder.Fullname.Replace($SRV1unc,$SRV2unc)).Replace('\\\','\\')) -Force -ErrorAction SilentlyContinue
        }
    }
}

##Files comparison
$ComparisonFiles = $Comparison | Where-Object {$_.PSIsContainer -eq $false}
$FilesPresentOnEach = $ComparisonFiles | Where-Object {($_.SideIndicator -eq "==")  -and ($_.LastWriteTime -eq (($_.Directory.Fullname.Replace($SRV2unc,$SRV1unc)).Replace('\\\','\\')).LastWriteTime)}
$FilesNotPresentInSRV1 = $ComparisonFiles | Where-Object {($_.SideIndicator -eq "=>") -and ((Test-Path($_.Directory.Fullname.Replace($SRV2unc,$SRV1unc)).Replace('\\\','\\')))}
$FilesNotPresentInSRV2 = $ComparisonFiles | Where-Object {($_.SideIndicator -eq "<=") -and ((Test-Path($_.Directory.Fullname.Replace($SRV2unc,$SRV1unc)).Replace('\\\','\\')))}
## Files not present on $SRV1
if ($FilesNotPresentInSRV1.Count -ne 0) {
    foreach ($File in $FilesNotPresentInSRV1) {
        Copy-Item -Path $File.FullName -Destination (($File.Directory.Fullname.Replace($SRV2unc,$SRV1unc)).Replace('\\\','\\')) -Force -ErrorAction SilentlyContinue
    }
}
##Files not present on $SRV2
if ($FilesNotPresentInSRV2.Count -ne 0) {
    foreach ($File in $FilesNotPresentInSRV2) {
        Copy-Item -Path $File.FullName -Destination (($File.Directory.Fullname.Replace($SRV1unc,$SRV2unc)).Replace('\\\','\\')) -Force -ErrorAction SilentlyContinue
    }
}
$Global:Component = "Files already present"
if ($FilesPresentOnEach.Count -ne 0) {
    foreach ($File in $FilesPresentOnEach | Where {$_.LastWriteTime -gt (Get-Item(($_.FullName.Replace($SRV1unc,$SRV2unc)).Replace('\\\','\\'))).LastWriteTime}) {
        Copy-Item -Path $File.FullName -Destination (($File.Fullname.Replace($SRV1unc,$SRV2unc)).Replace('\\\','\\')) -Force -ErrorAction SilentlyContinue       
    }
    foreach ($File in $FilesPresentOnEach | Where {$_.LastWriteTime -lt (Get-Item(($_.FullName.Replace($SRV1unc,$SRV2unc)).Replace('\\\','\\'))).LastWriteTime}) {  
        Copy-Item -Path (($File.Fullname.Replace($SRV1unc,$SRV2unc)).Replace('\\\','\\')) -Destination $File.FullName -Force -ErrorAction SilentlyContinue
    }
}

我希望在另一个il上复制了属性LastWriteTime越新的文件已经存在并且更旧,或者如果不存在。 实际上,某些文件夹没有被复制,并且无法按预期工作。

我不使用某些replace重写路径,而是如何正确执行呢? 也许我以错误的方式来解决问题,并使用其他工具来解决?

1 个答案:

答案 0 :(得分:0)

为什么要使其变得复杂,为什么要使其简单-阴影

感谢Ansgar Wiechers,使用不带镜像的Robocopy是完成任务的最简单方法。

robocopy $pathFTP2 $pathFTP1 /E /ZB /X /COPYALL /XO /FFT /LOG:$ficROBOCOPY
robocopy $pathFTP1 $pathFTP2 /E /ZB /X /COPYALL /XO /FFT /LOG+:$ficROBOCOPY

我现在必须处理一个gMSA帐户以启动任务和这种错误:

  

System.Management.Automation.ParameterBindingArgumentTransformationException:   无法对参数“值”进行参数转换。不能   将值转换为System.String类型。 ->   System.Management.Automation.ArgumentTransformationMetadataException:   无法将值转换为类型System.String。 ->   System.Management.Automation.PSInvalidCastException:无法转换   值以键入System.String。在   System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics   engineIntrinsics,对象inputData,布尔绑定参数,布尔   bindingScriptCmdlet)-内部异常堆栈跟踪的结尾-
  在   System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics   engineIntrinsics,对象inputData,布尔绑定参数,布尔   bindingScriptCmdlet)位于   System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal   参数,CompiledCommandParameter参数元数据,   ParameterBindingFlags标志)---内部异常堆栈跟踪的结尾   ---在System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext   funcContext,Exception异常)位于   System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame   帧)   System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame   帧)   System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame   框架)

但这是另一个问题...