PowerShell Azure Blob Storage Exlude Specific ContentType - azure

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.

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).

Powershell script to delete the .bak files older than 30 days in all containers in one single script

I am trying to use for loop for selecting all the containers using this script
param($TimerBlob)
$CleanupTime = [DateTime]::UtcNow.AddMinutes(-1)
$context = New-AzStorageContext -StorageAccountName **** -StorageAccountKey ****
$containername = #("mydbstore", "mydbstore2","mydbstore3") # List of containers -
For ($i=0; $i -lt $containername.Length; $i++) {
Get-AzStorageBlob -Container $containername[$i] -Context $context
Where-Object { $_.LastModified.UtcDateTime -lt $CleanupTime -and $_.BlobType -eq "PageBlob" -and $_.Name -like "*.bak"} |
Remove-AzStorageBlob
}
The output lists all the files in all containers but doesn't delete them
Curious, what is your output from the get-azstorageblob | ?{} without the pipe into remove-azstorageblob?
Are you getting a list of items that matches your filters?

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

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

Searching for filenames in blob using a wildcard

I am trying to find any file whose name begins with 'filename', but I cant work it out.
Get-AzureStorageBlob -Context $Context -Container $SourceContainer |
Get-ChildItem Where{$_.Name -like 'filename'}
Where am I going wrong? Thank you.
Try the command below, it works on my side.
Get-AzureStorageBlob -Context $Context -Container $SourceContainer |
Where-Object {$_.Name -like "filename*"}
Test result with no filter and filter:

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