2017-07-19 47 views
1

我正在使用powershell腳本將文件從1位置移動到另一個位置,然後我使用compare-object從兩個位置獲取散列值,並在出現問題時發出信號。compare-object不包括比x更早的文件時間量

由於get-hash需要很長時間(談到多GB數據),我想排除已經存在幾小時/天的文件。

我的代碼:

Compare-Object -ReferenceObject (dir $nas_smb_share -Recurse | Where-Object{!$_.psiscontainer} | get-hash) -differenceObject (dir $cs_dest -Recurse | Where-Object {!$_.psiscontainer} | get-hash) | 
%{if ($_.SideIndicator -eq "=>"){$result = ("$($_.InputObject)")}} 
if ([string]::IsNullOrEmpty($result)){$res = "Transfer succeeded without problems"} 
else {$res = ("transfer failed on following file(s): "+ (dir $cs_dest -Recurse | Where-Object {!$_.psiscontainer } | get-hash | ? {$_.hashstring -match $result}))} 

輸出是與文件亂碼的郵件被更改的值。

所以我想就如何改變這一部分的一些輸入:

Compare-Object -ReferenceObject (dir $nas_smb_share -Recurse | Where-object{!$_.psiscontainer} | get-hash) 

所以只需要文件/文件夾是不超過例如1小時

回答

1

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 
+0

Showoff ..... :-) – EBGreen

+0

@EBGreen:... :) – mklement0

1

你可以只篩選中老年您的where子句:

Compare-Object -ReferenceObject (dir $nas_smb_share -Recurse | Where-object{(!$_.psiscontainer) -AND ($_.LastWriteTime -gt (Get-Date).AddHours(-1))} | get-hash) 
相關問題