Error "InvalidOperation: /opt/microsoft/powershell/7/profile.ps1:64" in CloudShell

Juan-Pablo Garcia 5 Reputation points
2023-06-23T14:19:29.52+00:00

I receive the following error messages when I run a PowerShell script in Azure CloudShell. As you can see, the filename for my script is "TEST-JPG.ps1", and the first error appears to be coming from another script (profile.ps1) that is somehow called by one of the statements in my script. Interestingly, when I copy and paste the script's instructions to the command prompt, they run without any problems. Any ideas as to what might be wrong here?

Thanks in advance!

PS /home/juan> ./TEST-JPG.ps1     
InvalidOperation: /opt/microsoft/powershell/7/profile.ps1:64
Line |
  64 |      & ($script:PSCloudShellUtilityModuleInfo){param([string]$Label, [ …
     |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block,
     | or a CommandInfo object.
InvalidOperation: /opt/microsoft/powershell/7/profile.ps1:178
Line |
 178 |              $envName = $script:CloudEnvironmentMap[$env:ACC_CLOUD]
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.
Get-AzureADUser: /home/juan/TEST-JPG.ps1:10
Line |
  10 |  $users = Get-AzureADUser -All $true
     |           ~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You must call the Connect-AzureAD cmdlet before calling any other cmdlets.
Disconnect-AzureAD: /home/juan/TEST-JPG.ps1:41
Line |
  41 |  Disconnect-AzureAD
     |  ~~~~~~~~~~~~~~~~~~
     | Object reference not set to an instance of an object.
Disconnect-AzureAD: /home/juan/TEST-JPG.ps1:41
Line |
  41 |  Disconnect-AzureAD
     |  ~~~~~~~~~~~~~~~~~~
     | Object reference not set to an instance of an object.
PS /home/juan> 
Azure Cloud Services
Azure Cloud Services
An Azure platform as a service offer that is used to deploy web and cloud applications.
770 questions
Windows for business Windows Server User experience PowerShell
{count} vote

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-06-23T15:27:21.53+00:00
    0 comments No comments

  2. kobulloc-MSFT 26,801 Reputation points Microsoft Employee Moderator
    2023-06-24T00:42:16.3066667+00:00

    Hello, @Juan-Pablo Garcia !

    Why am I getting a You must call the Connect-AzureAD cmdlet before calling any other cmdlets. error with Azure Cloud Shell?

    Line 10 is what initially stands out when I look at this:

    You must call the Connect-AzureAD cmdlet before calling any other cmdlets.

    It used to be the case that Azure Cloud Shell ran Connect-AzureAD when starting up PowerShell however this is no longer run to speed up start time. You can choose to restore the previous behavior by adding Connect-AzureAD to the $PROFILE file in PowerShell.

    For more information, you can read the Azure Cloud Shell troubleshooting documentation:

    https://learn.microsoft.com/en-us/azure/cloud-shell/troubleshooting#error-running-azuread-cmdlets-in-powershell


    I hope this has been helpful! Your feedback is important so please take a moment to accept answers.

    If you still have questions, please let us know what is needed in the comments so the question can be answered. Thank you for helping to improve Microsoft Q&A!

    User's image


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.