I'm creating a process to delete SQL backup files from Azure blob storage.
Through my research it seems the only way for me to reliably stage the removal of backups older than X days is to read the file names into a text file and then create a second file with filtered results based on a string date included in the file name.
There are two date formats involved in this task: yyyy_MMdd and yyyyMMdd.
I need to find all files between 120 and 365 days old. There are over 50,000 files to process and about 500 different date strings to search for.
I know what I have currently can be improved because it takes forever to run (plus it's RBAR). How do I process all the files more efficiently?
Get-AzureStorageBlob -Context $context -Container "ContainerName" | SELECT name | Out-File -filepath C:\Users\name\Desktop\Deletions.txt
$Arr1 = @()
$Arr2 = @()
$collection = @()
ForEach($number in 120..365)
{
$Arr1 += (Get-Date).addDays(-$number).ToString("yyyy_MMdd")
$Arr2 += (Get-Date).addDays(-$number).ToString("yyyyMMdd")
}
$collection += $Arr1
$collection += $Arr2
ForEach ($date in $collection)
{
Get-Content -Path C:\Users\name\Desktop\Deletions.txt | Where-Object { $_ -like "*$date*" } | Out-File -filepath C:\Users\name\Desktop\DeleteThese.txt -Append
}
Using ArrayLists instead of arrays should give you a boost. You can create ArrayList objects like: $Arr1 = New-Object System.Collections.ArrayList
.
$Arr1 = @()
$Arr2 = @()
$collection = New-Object System.Collections.ArrayList
ForEach($number in 120..365)
{
$Arr1 += (Get-Date).addDays(-$number).ToString("yyyy_MMdd")
$Arr2 += (Get-Date).addDays(-$number).ToString("yyyyMMdd")
}
# have to add each item from the original arrays into the arraylist
# otherwise the arraylist will only have a count of 2, the entirety of $Arr1 and $Arr2 respectively
foreach($item in $Arr1) { $collection.Add($item) | Out-Null }
foreach($item in $Arr2) { $collection.Add($item) | Out-Null }
foreach($date in $collection)
{
Get-Content -Path C:\Users\name\Desktop\Deletions.txt | Where-Object { $_ -like "*$date*" } | Out-File -filepath C:\Users\name\Desktop\DeleteThese.txt -Append
}