EBGreen's answer爲您的逐個修改時間過濾問題提供了有效的解決方案。
讓我用一個更高效的簡化版代碼來補充它,它也解決了Compare-Object
調用的概念性問題。
注意:需要PSv4 +,由於使用Get-FileHash
(V4 +)和Get-ChildItem -File
(V3 +)和簡化Where-Object
語法(比較表; V3 +)。
# Get current time stamp.
$now = Get-Date
# Determine the cut-off time span.
$ts = New-TimeSpan -Hours 1
# Construct the filtering script block to pass to Where-Object below,
# which filters in only those files whose last-modified time is equal to
# or less than the time span.
$sb = { $now - $_.LastWriteTime -le $ts }
# Construct the path wildcard expression that we'll use to filter the
# Compare-Object output to find the right-side-only differences.
# Note: Because we use Compare-Object -PassThru below in order to preserve
# the [Microsoft.PowerShell.Commands.FileHashInfo] instances output by
# Get-FileHash, we don't have acces to the .SideIndicator property.
$inRightSideSubtreePattern = (Convert-Path $cs_dest) + '/*'
# Compare the file hashes and return those that are different on the right side.
# * Note the use of -File to limit Get-ChildItem's output to files.
# * Note the use of -Property Hash to ensure that the actual hash values are
# compared - without it, Compare-Object would simply compare the results of
# calling .ToString() on the input objects, which would yield the static
# 'Microsoft.PowerShell.Commands.FileHashInfo' string value, and no differences
# would ever be found.
# * Since we do want access to the .Path property of the
# [Microsoft.PowerShell.Commands.FileHashInfo] objects output Get-FileHash
# later, we have no choice but to use -PassTru - otherwise, we'd get a
# [pscustomobject] with a .SideIndicator property and *only* a .Hash property.
# * Conversely, however, using -PassThru - which passes the input objects through
# as-is - deprives of the .SideIndicator property, so the .Path property
# is used to find the right-side-only results.
$result = Compare-Object -PassThru -Property Hash `
(Get-ChildItem $nas_smb_share -File -Recurse | Where-Object $sb | Get-FileHash) `
(Get-ChildItem $cs_dest -File -Recurse | Get-FileHash) |
Where-Object Path -like $inRightSideSubtreePattern
# The [Microsoft.PowerShell.Commands.FileHashInfo] instances output by Get-FileHash
# have a .Path property containing the input file's full filename.
# Applying .Path to the $result as a whole will retrieve an array of the hashes'
# input filenames and, in a string context, concatenate them with spaces.
if ($result.Count -eq 0) {$res = "Transfer succeeded without problems"}
else {$res = "Transfer failed on following file(s): " + $result.Path }
# Output result.
$res
Showoff ..... :-) – EBGreen
@EBGreen:... :) – mklement0