Deleting older .bak files from Azure Storage Account using PowerShell Script

Victor R 0 Reputation points
2023-08-23T19:41:36.3333333+00:00

Hello,

I have following PS script. Testing for the first time. Bit new to Azure. When I run this I am seeing the error message shown below. Will greatly appreciate your help at the earliest. I am testing from inside a Azure VM (Win2k19 OS) and not from Azure portal.


param($TimerBlob)  
  
$CleanupTime = [DateTime]::UtcNow.AddHours(-168)  
$context = New-AzStorageContext -StorageAccountName "dailysqlbackup" -StorageAccountKey "<Access Key of the storage account>"
Get-AzStorageBlob -Container "dbbackups" -Context $context |   
Where-Object { $_.LastModified.UtcDateTime -lt $CleanupTime -and $_.BlobType -eq "PageBlob" -and $_.Name -like "*.bak"} |  
Remove-AzStorageBlob

Get-AzStorageBlob : Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. HTTP Status Code: 403 - HTTP

Error Message: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
ErrorCode: AuthenticationFailed
ErrorMessage: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId: d4a0417a-e01e-0018-77f9-d522d5000000
Time: Wed, 23 Aug 2023 14:39:22 GMT
At line:6 char:1
+ Get-AzStorageBlob -Container "dbbackups" -Context $context |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzStorageBlob], StorageException
    + FullyQualifiedErrorId : StorageException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.GetAzureStorageBlobCommand
 
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,543 questions
{count} votes

2 answers

Sort by: Most helpful
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2023-08-24T03:49:44.2733333+00:00

    @Victor R

    The above error message clearly indicates something is wrong with the authentication.

    Can you kindly check if you have copied the storage account key correctly?

    I just tried the same script to simulate your issue by providing invalid storage account key:

    Once I gave the correct key , the script went through fine...

    User's image

    Try to regenerate the storage key or use secondary key...

    Regards,

    Shiva.


  2. Victor R 0 Reputation points
    2023-09-05T13:42:07.9+00:00

    Hello Shiva,

    The Access Key is correct. Since I am using Credentials for running backup, if I am using incorrect Access Key in the Credentials, my backup job fails.

    Thanks.

    Victor

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.