immerkind
Goto Top

Powershell: Dateien nach Version löschen in mehreren Ordnern

Hallo zsm

Ich habe für mein Backup ein Skript geschrieben. Dieses soll am Ende des Skriptes im Zielordner nachschauen, wie viele Dateien es in jedem einzelnen Ordner gibt.
In diesen Unterordnern soll er die Dateien Sortieren und 30 überspringen. Alle die dann übrig sind soll er endgültig löschen.

Das ganze sieht bis jetzt so aus. Jedoch komme ich nicht weiter.

function getRecursedZIP($Folder, $Filemask="*.zip") {  
    $Results = Get-ChildItem $Folder -Filter $Filemask -Recurse -File
    return $Results
} 

$ZipFiles = getRecursedZIP -folder $DestinationArch -filemask "*.zip"  
$ZipFilesGruoped = $ZipFiles | group DirectoryName
ForEach ($file in $ZipFilesGruoped) {  
    $FilestoRemove = sort LastWriteTime -Descending | select -Skip $VersionArchive  
        try {
            remove-item -Path $FilestoRemove
        }catch {
            $errCount += 1
            Write-EventLog -LogName 'Logfile Archive IIS' -Source 'Logfile Archive' -EntryType Error -EventId 6 -Message "Error with delete archivefiles"  
        }
    }

Was genau mache ich falsch oder was habe ich vergessen?

Vielen Dank für eure Hilfe

Gruss
ImmerKind

Content-Key: 308700

Url: https://administrator.de/contentid/308700

Printed on: April 24, 2024 at 09:04 o'clock

Mitglied: 129813
129813 Jul 01, 2016 updated at 13:03:20 (UTC)
Goto Top
Hi.
You forgot to mention what should be sorted and skipped face-wink
Simply show yourself the output of the var $ZipFilesGruoped then you see how this object looks like and which properties it has!
# ....
$FilestoRemove = $file.Group | sort LastWriteTime -Descending | select -Skip $VersionArchive
if ($FilestoRemove){
    #....try catch ...
}
# ....
Regards
Member: ImmerKind
ImmerKind Jul 01, 2016 updated at 13:50:35 (UTC)
Goto Top
Ich habe das Problem selber lösen können. Danke trotzdem für die Hilfe face-smile

Lösung:

remove-item -Path $FilestoRemove.FullName
Mitglied: 129813
Solution 129813 Jul 01, 2016 updated at 13:55:32 (UTC)
Goto Top
But you also forgot $file.Group in your sorting pipeline.
You can also do
$FilesToRemove | remove-item -Force
to remove the Items, and you should pre check if the collection is empty or not, this would be a better style instead of letting it always run into the catch.
Member: ImmerKind
ImmerKind Jul 01, 2016 at 13:57:28 (UTC)
Goto Top
Yes you can do it also face-smile

Thanks for your help