How to set "Message never expires" expiry for queue message in Storage Queue using powershell?

Nagendrra C 61 Reputation points
2023-12-29T13:25:40.05+00:00

Hi,

I have been trying to set the "Message never expires" expiry for my queue message from PowerShell but I am getting an error when trying to do it.

The code looks like this:

$message = 'Some random message'
$queue.CloudQueue.AddMessageAsync($message, -1)

Here is the error:

Cannot find an overload for "AddMessageAsync" and the argument count: "2".
At line:1 char:1
+ $queue.CloudQueue.AddMessageAsync($queueMessage, -1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest


Am I doing something wrong? how do I set the value?

Thanks and Regards,

Nagendrra C

Azure Queue Storage
Azure Queue Storage
An Azure service that provides messaging queues in the cloud.
107 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,592 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anand Prakash Yadav 7,795 Reputation points Microsoft Vendor
    2024-01-04T12:06:09.6633333+00:00

    Nagendrra C, I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this!

    Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer. Accepted answers show up at the top, resulting in improved discoverability for others.
    User's image

    Issue: Customer unable to set "Message never expires" expiry for queue message in Storage Queue using PowerShell.

    Error Message:

    Cannot find an overload for "AddMessageAsync" and the argument count: "2".
    At line:1 char:1
    + $queue.CloudQueue.AddMessageAsync($queueMessage, -1)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    
    

    Solution: Customer has found the following PowerShell script useful for setting the expiry time of a queue message for an extended duration.

    # Getting queue reference
    $queue = New-AzStorageQueue -Name "queuename" -Context "StorageAccountContext"
    
    #Creating Queue message
    $Message = "Message here"
           
    $queueMessage =[Microsoft.Azure.Storage.Queue.CloudQueueMessage]::new($Message)
    $newTimeSpan = [TimeSpan]::new(5110.01,0,0)
    $queue.CloudQueue.AddMessage($queueMessage, $newTimeSpan)
    

2 additional answers

Sort by: Most helpful
  1. Azar 22,870 Reputation points MVP
    2023-12-30T03:38:19.89+00:00

    Hi Nagendra

    I have modified the expirationTime property of the message is set to "-1" to indicate that the message should never expire. Adjust the variables such as $connectionString and $queueName according to your specific setup.

    # Import the Azure.Storage.Queues module
    Import-Module Az
    # Set your storage account connection string
    $connectionString = "your_storage_account_connection_string"
    # Set the queue name
    $queueName = "your_queue_name"
    
    # Create a queue client
    $queueClient = [Microsoft.Azure.Storage.Queue.CloudStorageAccount]::Parse($connectionString).CreateCloudQueueClient()
    
    # Get a reference to the queue
    $queue = $queueClient.GetQueueReference($queueName)
    
    # Create a message with "Message never expires" property
    $messageContent = "Some random message"
    $queueMessage = New-Object Microsoft.Azure.Storage.Queue.CloudQueueMessage $messageContent
    $queueMessage.Properties["expirationTime"] = "-1" # Setting expirationTime to -1 means the message never expires
    
    # Add the message to the queue
    $queue.AddMessageAsync($queueMessage).Wait()
    
    
    

    If this helps kindly accept the answer thanks much.


  2. Anand Prakash Yadav 7,795 Reputation points Microsoft Vendor
    2024-01-02T12:41:45.42+00:00

    Hello Nagendrra C,

    Thank you for posting your query here!

    Please note that Az module in PowerShell may not provide a direct way to set a message to "never expire" in an Azure Storage Queue. The Time-To-Live (TTL) for a message in Azure Storage Queues is limited to a maximum of 7 days.

    Here is a PowerShell script to add a message with 7 days TTL:

    # Set your storage account details
    $storageAccountName = "Storage Account Name"
    $storageAccountKey = “Storage Account Access key" # or use SAS token or Managed Identity
    
    # Set the queue name
    $queueName = "Queue name"
    
    # Set the message content
    $messageContent = "Message"
    
    # Create a storage context
    $storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
    
    # Get a reference to the queue
    $queue = Get-AzStorageQueue -Name $queueName -Context $storageContext
    
    $queueMessage = [Microsoft.Azure.Storage.Queue.CloudQueueMessage]::new($messageContent)
    
    # Add the message to the queue
    $queue.CloudQueue.AddMessageAsync($queueMessage).Wait()
    
    Write-Host "Message added to the queue."
    

    However, the time-to-live interval for the message, in seconds. Prior to version 2017-07-29, the maximum time-to-live allowed is 7 days. For version 2017-07-29 or later, the maximum time-to-live can be any positive number, as well as -1 indicating that the message does not expire. If this parameter is omitted, the default time-to-live is 7 days. https://learn.microsoft.com/en-us/rest/api/storageservices/put-message#uri-parameters

    If your application requires a message to be effectively non-expiring, you may need to implement a workaround such as using .NET

    The following code snippet adds a message that never expires.

    To add a message that doesn't expire, use Timespan.FromSeconds(-1) in your call to SendMessageAsync.

    await theQueue.SendMessageAsync(newMessage, default, TimeSpan.FromSeconds(-1), default);
    

    For details on Azure Queue Storage client library for .NET https://learn.microsoft.com/en-us/azure/storage/queues/storage-quickstart-queues-dotnet?tabs=passwordless%2Croles-azure-portal%2Cenvironment-variable-windows%2Csign-in-powershell

    Also, if you add a message to the queue via Portal, you have the option to select Message never expires to indicate a message that will remain in the queue until it is explicitly removed.

    User's image

    Please let us know if you have any further queries. I’m happy to assist you further.


    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


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.