Automated Removal of Old Blob Storage with Powershell.
This was a request/refactoring of code from a DBA I know, and making it work in our environment.
Pretty simple, but still required a bit of tinkering.
Ensure the module is installed, and set the alias just incase. (For reusability)
Set the Containers and the Accounts.
Set the Time Context.
Minor note, the code I was working from was "like blob" but the script found no hits. Looking at the directory manually I saw everything was "blockblob" so that is what we aimed the script to remove.
Again this is pretty straightforward, Just took some time to get it just so.
Pretty simple, but still required a bit of tinkering.
Ensure the module is installed, and set the alias just incase. (For reusability)
Set the Containers and the Accounts.
Set the Time Context.
Minor note, the code I was working from was "like blob" but the script found no hits. Looking at the directory manually I saw everything was "blockblob" so that is what we aimed the script to remove.
Again this is pretty straightforward, Just took some time to get it just so.
#Ensure Azure Installed if (Get-Module -ListAvailable -Name AZ) { Write-Host "Azure Module Installed" } else { Write-Host "Installing Azure module" Install-Module Install-Module Az -Force -AllowClobber Import-Module Az } Enable-AzureRmAlias Connect-AzAccount $container="YOUR_CONTAINER" $StorageAccountName = "YOUR_ACCOUNT_NAME" $bakHourOld=-480 $logHourOld=-480 $context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey YOUR_KEY_HERE $CleanupTime = [DateTime]::UtcNow.AddHours($bakHourOld) Get-AzureStorageBlob -Container $container -Context $context | Where-Object { $_.LastModified.UtcDateTime -lt $CleanupTime -and $_.BlobType -eq "BlockBlob" -and $_.Name -like "*.bak"} | Remove-AzureStorageBlob $CleanupTime = [DateTime]::UtcNow.AddHours($logHourOld) Get-AzureStorageBlob -Container $container -Context $context | Where-Object { $_.LastModified.UtcDateTime -lt $CleanupTime -and $_.BlobType -eq "BlockBlob" -and $_.Name -like "*.log"} | Remove-AzureStorageBlob
Comments
Post a Comment