Meeting "ERROR: Run Connect-AzAccount to login. Exception : Type : System.Management.Automation.PSInvalidOp" In Azure powershell function when run "Connect-AzAccount -Environment AzureChinaCloud -Credential $Cred)"

JackChen 0 Reputation points
2024-08-15T01:20:23.8166667+00:00

Our China production environmen powershell function APP have ran normoally long time but in t report error suddenly. we have tried change Az module to some older and newer version, and rewite runtime version, but stll report the same error. Please help troubleshooting and resolve it.

Sample code as below (it runs successfully in local Env ) :

param($Timer)
$password="xxxxx"
$Creds = ((New-Object System.Management.Automation.PSCredential("xxxx@xxx.xxxx.onmschina.cn",(ConvertTo-SecureString $password -AsPlainText -Force))))

foreach ($Cred in $Creds)
{
     [void](Connect-AzAccount -Environment AzureChinaCloud  -Credential $Cred);
    $Subscriptions = Get-AzSubscription
foreach ($subscription in $subscriptions){ 	
    $subscription 
}
}

Error details:

ERROR: Run Connect-AzAccount to login. Exception : Type : System.Management.Automation.PSInvalidOperationException ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException Message : Run Connect-AzAccount to login. HResult : -2146233087 CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException FullyQualifiedErrorId : InvalidOperation TargetSite : Name : get_DefaultContext DeclaringType : Microsoft.Azure.Commands.ResourceManager.Common.AzureRMCmdlet MemberType : Method Module : Microsoft.Azure.PowerShell.Clients.ResourceManager.dll Message : Run Connect-AzAccount to login. Source : Microsoft.Azure.PowerShell.Clients.ResourceManager HResult : -2146233079 StackTrace : at Microsoft.Azure.Commands.ResourceManager.Common.AzureRMCmdlet.get_DefaultContext() at Microsoft.Azure.Commands.Profile.GetAzureRMSubscriptionCommand.BeginProcessing() at System.Management.Automation.Cmdlet.DoBeginProcessing() at System.Management.Automation.CommandProcessorBase.DoBegin() CategoryInfo : InvalidOperation: (:) [Get-AzSubscription], PSInvalidOperationException FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Profile.GetAzureRMSubscriptionCommand InvocationInfo : MyCommand : Get-AzSubscription ScriptLineNumber : 28 OffsetInLine : 22 HistoryId : 1 ScriptName : C:\home\site\wwwroot\TimerTrigger1\run.ps1 Line : $Subscriptions = Get-AzSubscription PositionMessage : At C:\home\site\wwwroot\TimerTrigger1\run.ps1:28 char:22 + $Subscriptions = Get-AzSubscription + ~~~~~~~~~~~~~~~~~~ PSScriptRoot : C:\home\site\wwwroot\TimerTrigger1 PSCommandPath : C:\home\site\wwwroot\TimerTrigger1\run.ps1 InvocationName : Get-AzSubscription CommandOrigin : Internal ScriptStackTrace : at 
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,890 questions
0 comments No comments
{count} votes

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.