To delete objects from an Azure Storage Blob in bulk which are 'x' days older - azure

I am looking to delete all files from azure storage blob which are older than 'x' days. I am trying the below code but is not working:
$StorageAccountName = '<name>'
$StorageAccountKey = '<key>'
$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzureStorageBlob -Container "reports" -Context $Ctx -Blob *.csv
where {$_.LastModified -le (get-date).AddDays(-30) } | Remove-AzureStorageBlob
I referred the following doc but the query is not working for conditional deletion. link

I suggest you use the new azure powershell module AZ.
After install the new AZ module, try the code below:
$accountname="xx"
$accountkey="xxx"
$ctx = New-AzStorageContext -StorageAccountName $accountname -StorageAccountKey $accountkey
Get-AzStorageBlob -Container "aa1" -Blob *.jpg -Context $ctx | where {$_.LastModified -le (Get-Date).AddDays(-1)} | Remove-AzStorageBlob
After the code running, you can check on azure portal or use Get-AzStorageBlob cmdlet to see if all the specified files are deleted. In my case, all the files' date < "1 day ago" are deleted.

Azure storage have feature "Manage the Azure Blob storage lifecycle".
https://learn.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts
For your test case you can directly refer
https://learn.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts#powershell
$action = Add-AzStorageAccountManagementPolicyAction -BaseBlobAction Delete -daysAfterModificationGreaterThan 2555

Thank you Ivan. I compared my script with yours and found that I was missing a pipe before the where condition which was issue. After putting the pipe, I am able to delete the files based on condition. Didn't needed to go to AzureAz.
The script which is working now is :
$StorageAccountName = 'xx'
$StorageAccountKey = 'yyy'
$ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzureStorageBlob -Container "abc" -Blob *.pdf -Context $ctx | where {$_.LastModified -le (Get-Date).AddDays(-4)} | Remove-AzureStorageBlob

Related

Need to timeout Get-AzureStorageBlob function

I am using the Get-AzStorageBlob command to pull out blob files
https://learn.microsoft.com/en-us/powershell/module/az.storage/get-azstorageblob?view=azps-9.2.0
I want to ignore large-size blobs as they take an eternity to process.
Do we have a way where if this command does not yield output in 10 sec should error out?
try{
$blobs = ($container | Get-AzStorageBlob -context $context -ServerTimeoutPerRequest 10 -ClientTimeoutPerRequest 10).LastModified
}
catch {write-output "$($container) timed out"}
I tried adding parameters ServerTimeoutPerRequest & ClientTimeoutPerRequest but they seem to not help at all as the command is not erroring out after 10 sec.
I tried to reproduce the same in my environment to skip the large files in Blob:
This is one approach to skip the large files using PowerShell.
Based on filesize limit condition, we can fetch the files satisfying the size limit, if any file falls under beyond this size limit will be skipped in fetching.
I have uploaded 6 Files into my blob.
Here is the script:
Connect-AzureAD
Install-Module Az.Storage
$MaxReturn = 10000
$ContainerName = "thejademo"
$Token = $Null
$StorageContext = New-AzureStorageContext -StorageAccountName 'venkatsa' -StorageAccountKey 'ky3ewvDbRRMKqVWD75mQXAeojowkYUQFzVSItmgVosrygIG+ITJsrGRgAlVcqo2sY0zlcU2QVNMu+AStJrzWDA=='
$Container = Get-AzureStorageContainer -Name 'thejademo' -Context $StorageContext
$Blobs = Get-AzStorageBlob -Container $ContainerName -MaxCount $MaxReturn -ContinuationToken $Token -Context $StorageContext |where {($_.Length -lt 71458173)}
$Total = $Blobs.count
$Skip= "Skipped Large Blob Files"
Write-Host Total Files $Total in container $ContainerName $skip
Total 6 files in my container but displayed only 4 files (skipped large files).

Exporting VHD file from OS-Disk to storage account

I am trying to export VHD file from managed os-disk from southeast Asia to East Us location but gives me error 403 Authentication Failed "Copy failed when reading the source".
I am a global admin for tenant and Owner on subscription. Please find the screenshot and script for the same.
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName "Prod-Sub"
$RGName = "r-d-server-RG-2"
$DiskName = "r-d-server2-OSDisk-13052020"
$StorageAccount = "migratestorageserver2"
$STGKey = "dgosW21YW1kCyPLfhjf7Qhj14XMvRqqFB9Usp4jnjOPjJLRdwhfqBa3mJxHDnE7i479BUFQSK0vefgv/GKaYUA=="
$ContainerName = "migratecontainer"
$VHDName = "r-d-servermigrated.vhd"
$sas = Grant-AzureRmDiskAccess -ResourceGroupName $RGName -DiskName $DiskName -DurationInSecond 9600
-Access Read
$destContext = New-AzureStorageContext –StorageAccountName $StorageAccount -StorageAccountKey $STGKey
$blob1 = Start-AzureStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $ContainerName -
DestContext $destContext -DestBlob $VHDName
#Retrieve the current status of the blob copy operation
$status = $blob1 | Get-AzureStorageBlobCopyState
#Print out status
$status
#Loop until complete
While($status.Status -eq "Pending"){
$status = $blob1 | Get-AzureStorageBlobCopyState
Start-Sleep 10
### Print out status ###
$status
}
Most codes are right, I just need to recommend you turn into using the Az module. But the last while loop, it's not the right logic. The right logic is to get the fresh status list this:
While($status.Status -eq "Pending"){
Start-Sleep 10
### Print out status ###
$status = Get-AzStorageBlob -Context $destContext -Blob $VHDName -Container $ContainerName | Get-AzStorageBlobCopyState
}
And you should stop the VM before copy.

PowerShell Azure Blob Storage Exlude Specific ContentType

I am trying to list all blobs that are not PDF files. How would I do that?
I know that I can list all files like this:
$StorageContext = New-AzStorageContext -StorageAccountKey XXX -StorageAccountName YYY
Get-AzStorageBlob -Container 'containerName' -Context $StorageContext
And if I want to fitler for a specific content type, png in the case below, I would change the second line to this:
Get-AzStorageBlob -Container 'containerName' -Context $StorageContext -Blob *.png
But how can I display anything besides a specific content type. In my case I would want to list everything besides PDFs?
You could use the command below.
Get-AzStorageBlob -Container 'containerName' -Context $StorageContext | Where-Object {!($_.Name -like '*.pdf')}
I extended the version from #JoyWan to apply another filter. I want to show all files less than some date:
$filterDate = (Get-Date).AddMonths(-3) # get date 3 moths before
$StorageContext = New-AzStorageContext -StorageAccountKey XXX -StorageAccountName YYY
$blobs = Get-AzStorageBlob -Container 'containerName' -Context $StorageContext | where {($_.Name -notlike '*.pdf' -and $_.LastModified -le $filterDate)}
This example would show all files older than 3 months.

Is it possible to use Azure Automation Runbook to delete another Runbook output (an Azure File share snapshot)?

I want to use the runbook to delete another runbook output (an Azure File Share snapshot).
Is it possible? If you know something, please write something at here
Runbook 1: Create an Azure File share snapshot
$context = New-AzureStorageContext -StorageAccountName -StorageAccountKey
$share = Get-AzureStorageShare -Context
$context -Name "sharefile"
$snapshot = $share.Snapshot()
Runbook 2: Delete the Azure runbook output. The problem with this is that it deletes all snapshots rather than just delete the one created by the first runbook.
$allsnapshots = Get-AzureStorageShare -Context $context | Where-Object { $_.Name -eq "sharefile" -and $_.IsSnapshot -eq $true }
foreach($snapshot in $allsnapshots){
if($snapshot.SnapshotTime -lt (get-date).Add·Hours()){
$snapshot.Delete()
}
}
The sample code is as below, I test it in runbook and works well(create a snapshot, and then delete it after 3 minutes), and the other snapshots have no effect.
code in my powershell runbook:
param(
[string]$username,
[string]$password,
[string]$filesharename
)
$context = New-AzureStorageContext -StorageAccountName $username -StorageAccountKey $password
$share = Get-AzureStorageShare -Context $context -Name $filesharename
$s = $share.snapshot()
#get the snapshot name, which is always a UTC time formated value
$s2= $s.SnapshotQualifiedStorageUri.PrimaryUri.ToString()
#the $snapshottime is actually equal to snapshot name
$snapshottime = $s2.Substring($s2.IndexOf('=')+1)
write-output "create a snapshot"
write-output $snapshottime
#wait 180 seconds, then delete the snapshot
start-sleep -s 180
write-output "delete the snapshot"
$snap = Get-AzureStorageShare -Context $context -SnapshotTime $snapshottime -Name $filesharename
$snap.Delete()
write-output "deleted successfully after 3 minutes"
after it's running, you can see the snapshot is created in azure portal:
After it completes, the specified snapshot is deleted(you may need to open a new webpage to see the change due to some cache issue)
the output in runbook:

How to get size of Azure Container in PowerShell

Similar to this question How to get size of Azure CloudBlobContainer
How can one get the size of the Azure Container in PowerShell. I can see a suggested script at https://gallery.technet.microsoft.com/scriptcenter/Get-Billable-Size-of-32175802 but want to know if there is a simpler way to do in PowerShell
With Azure PowerShell, you can list all blobs in the container with Get-AzureStorageBlob with Container and Context parameter like:
$ctx = New-AzureStorageContext -StorageAccountName youraccountname -storageAccountKey youraccountkey
$blobs = Get-AzureStorageBlob -Container containername -Context $ctx
Output of Get-AzureStorageBlob is an array of AzureStorageBlob, which has a property with name ICloudBlob, you can get blob length in its Properties, then you can sum length of all blobs to get content length of the container.
The following PowerShell script is a simple translation of the c# code in the accepted answer of the question How to get size of Azure CloudBlobContainer. Hope this suit your needs.
Login-AzureRmAccount
$accountName = "<your storage account name>"
$keyValue = "<your storage account key>"
$containerName = "<your container name>"
$storageCred = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials ($accountName, $keyValue)
$storageAccount = New-Object Microsoft.WindowsAzure.Storage.CloudStorageAccount ($storageCred, $true)
$container = $storageAccount.CreateCloudBlobClient().GetContainerReference($containerName)
$length = 0
$blobs = $container.ListBlobs($null, $true, [Microsoft.WindowsAzure.Storage.Blob.BlobListingDetails]::None, $null, $null)
$blobs | ForEach-Object {$length = $length + $_.Properties.Length}
$length
Note: the leading Login-AzureRmAccount command will load the necessary .dll for you. If you do know the path of "Microsoft.WindowsAzure.Storage.dll", you can replace it by [Reflection.Assembly]::LoadFile("$StorageLibraryPath") | Out-Null. The path is usually like this "C:\Program Files\Microsoft SDKs\Azure.NET SDK\v2.7\ToolsRef\Microsoft.WindowsAzure.Storage.dll"
Here's my solution I just hammered through today. Above examples didn't give me what I wanted which was (1) a byte sum of all blobs in a container and (2) a list of each blob + path + size so that it can be used to compare the results to a du -b on linux (origin).
Login-AzureRmAccount
$ResourceGroupName = ""
$StorageAccountName = ""
$StorageAccountKey = ""
$ContainerName = ""
New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
# Don't NEED the Resource Group but, without it, fills the screen with red as it search each RG...
$size = 0
$blobs = Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ErrorAction Ignore | Get-AzureStorageBlob -Container $ContainerName
foreach ($blob in $blobs) {$size = $size + $blob.length}
write-host "The container is $size bytes."
$properties = #{Expression={$_.Name};Label="Name";width=180}, #{Expression={$_.Length};Label="Bytes";width=80}
$blobs | ft $properties | Out-String -width 800 | Out-File -Encoding ASCII AzureBlob_files.txt
I then moved the file to Linux to do some flip flopping of it and the find output to create a list of files to input into blobxfer. Solution to a different problem, but perhaps a suitable solution for your needs as well.

Resources