2017-07-19 3 views
1

Ich benutze ein Powershell-Skript, um Dateien von einem Ort zu einem anderen zu verschieben, dann verwende ich compare-object, um einen Hash-Wert von beiden Standorten zu erhalten und signalisieren, wenn ein Problem vorliegt.compare-object ohne Dateien älter als x Zeit

Da die get-hash eine lange Zeit dauert (sprechen von mehreren GB Daten) möchte ich Dateien ausschließen, die bereits für Stunden/Tage vorhanden sind.

mein Code:

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}))} 

Die Ausgabe ist eine Mail mit den Dateien Hash-Wert, der geändert wird.

Also würde ich eine Eingabe an mag, wie dieser Teil zu ändern:

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

so dauert es nur Dateien/Ordner, die nicht älter sind als zum Beispiel 1 Stunde

Antwort

1

EBGreen's answer bietet eine effektive Lösung für Ihre Filter-by-last-modified-Zeitproblem.

Lassen Sie mich ergänzen Sie mit einer effizienteren, stromlinienförmigen Version Ihres Codes, die auch ein konzeptionelles Problem mit Ihrem Compare-Object Anruf behandelt.

Anmerkung: Erfordert PSv4 +, aufgrund der Verwendung von Get-FileHash (v4 +) und Get-ChildItem -File (v3 +) und die vereinfachten Where-Object Syntax (Vergleich Anweisung; 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

Anschauen ..... :-) – EBGreen

+0

@EBGreen: ... :) – mklement0

1

Sie können einfach auswählen, in Ihre where-Klausel:

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