skip to Main Content

I am trying to execute the following powershell script via Azure ARM template (snippet below),

Install-Module -Name AzureAD -force

Import-Module -Name AzureAD -UseWindowsPowerShell -RequiredVersion 2.0.2.89 -force

$SecurePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($username, $SecurePassword)
Connect-AzureAD -Credential $credentials -Verbose -Debug

And getting the following error.

{
    "status": "failed",
    "error": {
        "code": "DeploymentScriptError",
        "message": "The provided script failed with the following error:rnSystem.Management.Automation.CommandNotFoundException: The term 'Connect-AzureAD' is not recognized as a name of a cmdlet, function, script file, or executable program.nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.n   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)n   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)n   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)n   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)n   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)n   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)n   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)n   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)n   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)n   at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)n   at Microsoft.PowerShell.Commands.InvokeExpressionCommand.ProcessRecord()n   at System.Management.Automation.Cmdlet.DoProcessRecord()n   at System.Management.Automation.CommandProcessor.ProcessRecord()rnat <ScriptBlock>, /mnt/azscripts/azscriptinput/removeappregistrationurl.ps1: line 33rnat <ScriptBlock>, <No file>: line 1rnat <ScriptBlock>, /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 295. Please refer to https://aka.ms/DeploymentScriptsTroubleshoot for more deployment script information."
    }
}

ARM template snippet

{
    "type": "Microsoft.Resources/deploymentScripts",
    "apiVersion": "2020-10-01",
    "name": "RemoveAppRegistrationUrl",
    "location": "eastus",
    "kind": "AzurePowerShell",
    "properties": {
        "forceUpdateTag": "1",
        "containerSettings": {
            "containerGroupName": "parsagecustomaci"
        },
        "azPowerShellVersion": "7.2",
        "arguments": "[concat(' -pUid ',parameters('UserId'),' -pPassword ',parameters('Password'),' -appId ',parameters('AppId'), ' -objId ', parameters('ObjectId'), ' -replyUrlToRemove ', parameters('ReplyUrlToRemove'))]",
        "primaryScriptUri": "https://saproliosaasdev.blob.core.windows.net/armtemplates/removeappregistrationurl.ps1",
        "supportingScriptUris": [],
        "timeout": "PT30M",
        "cleanupPreference": "OnSuccess",
        "retentionInterval": "P1D"
    }
}

I am able to run the commands from my local powershell instance and Azure cloud shell without any issues.

2

Answers


  1. The term ‘Connect-AzureAD’ is not recognized as a name of a cmdlet, function, script file, or executable program.nCheck the spelling of the name.

    The error message indicates that the Connect-AzureAD is not installed on your machine. if the AzureAD module is not imported correctly.

    You can follow the below steps to troubleshoot the Azure AD module.

    1. You can verify that the AzureAD module is installed correctly by running the following command.

      Get-Installedmodule | Where-Object {$_.Name -match "AzureAD"}

    enter image description here

    You can also verify the Connect-AzureAD cmdlet is present on your system using following command.

    Find-Command | Where-Object {$_.Name -match "Connect-AzureAD"}
    

    enter image description here

    1. If the module is not listed, then you need to install it using the following command

      Install-Module -Name AzureAD

    Make sure that confirm the installation by entering “A” – Yes to All.

    enter image description here

    1. Once you install the module, make sure to import it using the following command

      Import-Module -Name AzureAD

    2. Update the AzureAD module using the following command

      `Update-module AzureAd`
      
    3. Once you update the AzureAD module, try running the Connect-AzureAD.

    Login or Signup to reply.
  2. Did you find a solution this problem? I don’t think others realized that you are deploying the via ARM Template and you already have it working on your local PowerShell console.

    You do not need to run ‘Connect-AzureAD’ because it is already running in the proper context when deploying it from the ARM Template.

    You will however need to install and import the module. Installing the module seems to work fine, however importing the module will result in a truncating JSON error. I believe this is being caused by loading the module into memory and it consumes the limitation of 2. I am unsure if the limitation can be altered.

    I think the work around is to use Azure CLI. It already supports most Azure AD actions you need. Of course, you will need to do some homework and figure out what those commands are and how to use them. (Assuming you are like me and dont script using it often).

    Keep in mind, you will not need to connect using Azure CLI either. Just run the commands as if you were already connected.

    Note:
    If you are interested here is Microsoft Arm Template example. They are using deployment script to deploy Azure AD objects using Azure CLI in the ARM Deployment script.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search