mirror of
https://dev.azure.com/effectory/Survey%20Software/_git/Cloud%20Engineering
synced 2026-02-27 18:52:18 +01:00
Merged PR 56817: added removeblobtags automation account runbook
- added removeblobtags automation account runbook - Merge branch 'master' into jurjen/removeblobtags Related work items: #106056
This commit is contained in:
71
Powershell/RunBooks/RemoveBlobTags.ps1
Normal file
71
Powershell/RunBooks/RemoveBlobTags.ps1
Normal file
@@ -0,0 +1,71 @@
|
||||
param (
|
||||
[Parameter(mandatory=$true)]
|
||||
[string] $storageName,
|
||||
[Parameter(mandatory=$true)]
|
||||
[string] $subscriptionId,
|
||||
[Parameter(mandatory=$false)]
|
||||
[int]$maxIterations=2,
|
||||
[Parameter(mandatory=$false)]
|
||||
[int]$maxBlobPerIteration = 1000
|
||||
)
|
||||
|
||||
Import-Module Az.Storage
|
||||
|
||||
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
|
||||
[int] $total = 0
|
||||
|
||||
try {
|
||||
# This scripts removes all blob tags from an azure blob storage container based on a tag filter in the script. It performs it in chunks of 1000.
|
||||
# If there are no files left with the tag criteria the script will terminate
|
||||
# see: https://github.com/m4m4m4/CleanBlobTags/tree/main
|
||||
# see: https://www.reddit.com/r/AZURE/comments/1gvmulv/azure_blob_storage_malware_scan_and_blob_index/
|
||||
|
||||
# Connect to Azure with system-assigned managed identity
|
||||
Disable-AzContextAutosave -Scope Process
|
||||
Connect-AzAccount -Identity
|
||||
|
||||
# Set and store context
|
||||
Set-AzContext -SubscriptionId $subscriptionId
|
||||
|
||||
# Connect to the Azure Storage account
|
||||
$context = New-AzStorageContext -StorageAccountName $storageName -UseConnectedAccount
|
||||
|
||||
$token = $Null
|
||||
Do
|
||||
{
|
||||
#Retrieve blobs
|
||||
$blobs = Get-AzStorageBlobByTag -Context $context -TagFilterSqlExpression """Malware Scanning scan time UTC"">'0'" -MaxCount $maxBlobPerIteration -ContinuationToken $token
|
||||
|
||||
$blobCount = 1
|
||||
|
||||
#Loop through the batch
|
||||
Foreach ($blob in $blobs)
|
||||
{
|
||||
# Remove tags, as there really should 0 tags
|
||||
Set-AzStorageBlobTag -Context $context -Container $blob.BlobBaseClient.BlobContainerName -Blob $blob.Name -Tag @{} | out-null
|
||||
|
||||
#Display progress bar
|
||||
$percent = $($blobCount/$maxBlobPerIteration*100)
|
||||
Write-Progress -Activity "Processing blobs" -Status "$percent% Complete" -PercentComplete $percent
|
||||
$blobCount++
|
||||
}
|
||||
|
||||
#Update $total
|
||||
$total += $blobs.Count
|
||||
|
||||
#Exit if all blobs processed
|
||||
If($blobs.Length -le 0) { Break; }
|
||||
|
||||
#Set continuation token to retrieve the next batch
|
||||
$token = $blobs[$blobs.Count -1].ContinuationToken
|
||||
$maxIterations--
|
||||
}
|
||||
While ($null -ne $token -and $maxIterations -gt 0)
|
||||
}
|
||||
catch
|
||||
{
|
||||
Write-Error $_
|
||||
}
|
||||
|
||||
$stopwatch.Stop()
|
||||
Write-Output "Processed $total blobs in $($stopwatch.Elapsed)."
|
||||
Reference in New Issue
Block a user