2017-07-19 15 views
1

powershellスクリプトを使用して1つの場所から別の場所にファイルを移動していますが、問題がある場合は両方の場所と信号からハッシュ値を取得するためにcompare-objectを使用しています。xよりも古いファイルを除外するcompare-object

get-hashには長い時間がかかります(複数のデータを1つのデータとして扱っています)ので、すでに数時間/日の間に存在するファイルは除外したいと思います。

私のコード:

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

EBGreen's answerは、最後の修正時の問題に効果的なソリューションを提供します。

Compare-Objectコールでの概念的な問題に対処する、より効率的で合理的なコードでコードを補足してください。

注:原因Get-FileHash(V4の+)の使用とGet-ChildItem -File(V3 +)と簡略化Where-Object構文(; V3の+比較声明)に、PSV4 +が必要です。

# 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

あなただけでフィルタリングすることができ、例えば1時間以上経過したファイル/フォルダをとります。

は、だから私はこの部分を変更する方法についていくつかの入力を希望しますあなたのwhere句:

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