Skip to content

You cannot call a method on a null-valued expression. When calling $queue.CloudQueue.AddMessageAsync #27340

@bautistagr

Description

@bautistagr

Description

Hi,

Following PS script is run on Azure DevOps using Microsoft-hosted agents (windows-2025).
Basically I'm trying to send a message to an SA queue using PS Az module, but I'm getting this error:

##[error]You cannot call a method on a null-valued expression.

This is the PS script, line in bold is the one throwing the error

$azureStorageContext = New-AzStorageContext -StorageAccountName $(StorageAccountName) -StorageAccountKey $(StorageAccountKey)

$queue = Get-AzStorageQueue -Name $(QueueName) -Context $azureStorageContext

$queueItem = "my message"
$queueMessage = New-Object -TypeName Microsoft.Azure.Storage.Queue.CloudQueueMessage -ArgumentList "$queueItem"
$queue.CloudQueue.AddMessageAsync($queueMessage)

Could you tell me what I'm missing to make it work?

Issue script & Debug output

#Script

$azureStorageContext = New-AzStorageContext -StorageAccountName $(StorageAccountName) -StorageAccountKey $(StorageAccountKey)

$queue = Get-AzStorageQueue -Name $(QueueName) -Context $azureStorageContext

$queueItem = "my message"
$queueMessage = New-Object -TypeName Microsoft.Azure.Storage.Queue.CloudQueueMessage -ArgumentList "$queueItem"
$queue.CloudQueue.AddMessageAsync($queueMessage)


#Output

Expires:-1
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] ScopeSet was missing from the token response, so using developer provided scopes
 in the result. 
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] Checking client info returned from the server..
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] Saving token response to cache..
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] [SaveTokenResponseAsync] ID Token not present in response. 
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] Cannot determine home account ID - or id token or no client info and no subject 
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] [SaveTokenResponseAsync] Saving AT in cache and removing overlapping ATs...
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] Looking for scopes for the authority in the cache which intersect with 
https://storage.azure.com/.default
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z] 
[Internal cache] Total number of cache partitions found while getting access tokens: 0
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] Intersecting scope entries count - 0
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] 
 === Token Acquisition finished successfully:
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13]  AT expiration time: 3/14/2025 7:49:48 PM +00:00, scopes: 
https://storage.azure.com/.default. source: IdentityProvider
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] Fetched access token from host login.microsoftonline.com. 
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] 

[LogMetricsFromAuthResult] Cache Refresh Reason: NoCachedAccessToken

[LogMetricsFromAuthResult] DurationInCacheInMs: 0

[LogMetricsFromAuthResult] DurationTotalInMs: 323

[LogMetricsFromAuthResult] DurationInHttpInMs: 279
DEBUG: False MSAL 4.65.0.0 MSAL.CoreCLR .NET Framework 4.8.9290.0 Microsoft Windows 10.0.26100  [2025-03-14 18:49:49Z -
 08f966c1-bfbc-4362-bb97-9227a679ec13] TokenEndpoint: ****
DEBUG: ClientSecretCredential.GetToken succeeded. Scopes: [ https://storage.azure.com/.default ] ParentRequestId:  
ExpiresOn: 2025-03-14T19:49:48.1273929+00:00
DEBUG: AzureQoSEvent:  Module: Az.Storage:7.4.0; CommandName: New-AzStorageContext; PSVersion: 5.1.26100.2161; 
IsSuccess: True; Duration: 00:00:00.6489871; SanitizeDuration: 00:00:00.0010862
DEBUG: 6:49:49 PM - NewAzureStorageContext end processing.
DEBUG: 6:49:49 PM - Init Operation Context for 'GetAzureStorageQueueCommand' with client request id  
Azure-Storage-PowerShell-69b14f36-6a3e-426e-aade-f7bfe162c86c. If you want to get more details, please add "-Debug" to 
your command.
DEBUG: 6:49:49 PM - GetAzureStorageQueueCommand begin processing with ParameterSet 'QueueName'.
DEBUG: 6:49:49 PM - Use storage account 'mySA' from storage context.
DEBUG: Request [7a8e59ae-e64c-4b13-a0e4-00a00337d5f2] GET 
https://mySA.queue.core.windows.net/myqueue?comp=metadata

x-ms-version:2018-11-09

Accept:application/xml

User-Agent:AzurePowershell/v1.0.0,azsdk-net-Storage.Queues/12.16.0 (.NET Framework 4.8.9290.0; Microsoft Windows 
10.0.26100 )

x-ms-client-request-id:7a8e59ae-e64c-4b13-a0e4-00a00337d5f2

x-ms-return-client-request-id:true

Authorization:REDACTED

client assembly: Azure.Storage.Queues
DEBUG: Response [7a8e59ae-e64c-4b13-a0e4-00a00337d5f2] 200 OK (00.3s)

x-ms-request-id:706c7c8b-9003-004c-5511-956198000000

x-ms-version:2018-11-09

x-ms-approximate-messages-count:0

Cache-Control:no-cache

Date:Fri, 14 Mar 2025 18:49:48 GMT

Server:Windows-Azure-Queue/1.0,Microsoft-HTTPAPI/2.0

Content-Length:0
DEBUG: Request [1c919559-6307-4ee4-8c70-f7ef75b9f627] GET 
https://mySA.queue.core.windows.net/myqueue?comp=metadata

x-ms-version:2018-11-09

Accept:application/xml

User-Agent:AzurePowershell/v1.0.0,azsdk-net-Storage.Queues/12.16.0 (.NET Framework 4.8.9290.0; Microsoft Windows 
10.0.26100 )

x-ms-client-request-id:1c919559-6307-4ee4-8c70-f7ef75b9f627

x-ms-return-client-request-id:true

Authorization:REDACTED

client assembly: Azure.Storage.Queues
DEBUG: Response [1c919559-6307-4ee4-8c70-f7ef75b9f627] 200 OK (00.1s)

x-ms-request-id:706c7cea-9003-004c-2c11-956198000000

x-ms-version:2018-11-09

x-ms-approximate-messages-count:0

Cache-Control:no-cache

Date:Fri, 14 Mar 2025 18:49:48 GMT

Server:Windows-Azure-Queue/1.0,Microsoft-HTTPAPI/2.0

Content-Length:0
DEBUG: 6:49:49 PM - GetAzureStorageQueueCommand end processing, Start 0 remote calls. Finish 0 remote calls. Elapsed 
time 497.98 ms. Client operation id: Azure-Storage-PowerShell-69b14f36-6a3e-426e-aade-f7bfe162c86c.
DEBUG: AzureQoSEvent:  Module: Az.Storage:7.4.0; CommandName: Get-AzStorageQueue; PSVersion: 5.1.26100.2161; IsSuccess:
 True; Duration: 00:00:00.4943169; SanitizeDuration: 00:00:00.0010272
DEBUG: 6:49:49 PM - GetAzureStorageQueueCommand end processing.
##[error]You cannot call a method on a null-valued expression.
##[error]PowerShell exited with code '1'.

Environment data

Key   : PSVersion
Value : 5.1.26100.2161
Name  : PSVersion


Key   : PSEdition
Value : Desktop
Name  : PSEdition


Key   : PSCompatibleVersions
Value : {1.0, 2.0, 3.0, 4.0...}
Name  : PSCompatibleVersions


Key   : BuildVersion
Value : 10.0.26100.2161
Name  : BuildVersion


Key   : CLRVersion
Value : 4.0.30319.42000
Name  : CLRVersion


Key   : WSManStackVersion
Value : 3.0
Name  : WSManStackVersion


Key   : PSRemotingProtocolVersion
Value : 2.3
Name  : PSRemotingProtocolVersion


Key   : SerializationVersion
Value : 1.1.0.1
Name  : SerializationVersion

Module versions

Name              : Az.Accounts
Path              : C:\Modules\az_12.4.0\Az.Accounts\4.0.2\Az.Accounts.psm1
Description       : Microsoft Azure PowerShell - Accounts credential management cmdlets for Azure Resource Manager in 
                    Windows PowerShell and PowerShell Core.
                    
                    For more information on account credential management, please visit the following: 
                    https://learn.microsoft.com/powershell/azure/authenticate-azureps
Guid              : 17a2feff-488b-47f9-8729-e2cec094624c
Version           : 4.0.2
ModuleBase        : C:\Modules\az_12.4.0\Az.Accounts\4.0.2
ModuleType        : Script
PrivateData       : {PSData}
AccessMode        : ReadWrite
ExportedAliases   : {[Add-AzAccount, Add-AzAccount], [Get-AzDomain, Get-AzDomain], [Invoke-AzRest, Invoke-AzRest], 
                    [Login-AzAccount, Login-AzAccount]...}
ExportedCmdlets   : {[Add-AzEnvironment, Add-AzEnvironment], [Clear-AzConfig, Clear-AzConfig], [Clear-AzContext, 
                    Clear-AzContext], [Clear-AzDefault, Clear-AzDefault]...}
ExportedFunctions : {}
ExportedVariables : {}
NestedModules     : {Microsoft.Azure.PowerShell.Cmdlets.Accounts}

Error output

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue requires a change to an existing behavior in the product in order to be resolved.customer-reported

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions