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.
Was genau mache ich falsch oder was habe ich vergessen?
Vielen Dank für eure Hilfe
Gruss
ImmerKind
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
Please also mark the comments that contributed to the solution of the article
Content-Key: 308700
Url: https://administrator.de/contentid/308700
Printed on: April 24, 2024 at 09:04 o'clock
4 Comments
Latest comment
Hi.
You forgot to mention what should be sorted and skipped
Simply show yourself the output of the var $ZipFilesGruoped then you see how this object looks like and which properties it has!
Regards
You forgot to mention what should be sorted and skipped
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 ...
}
# ....
But you also forgot $file.Group in your sorting pipeline.
You can also do
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.
You can also do
$FilesToRemove | remove-item -Force