azure - 通过 ARM 模板运行 powershell 命令会抛出 "The term ' Connect-AzureAD' 未被识别为 cmdlet 的名称”错误

标签 azure powershell azure-active-directory azure-powershell azure-rm-template

我正在尝试通过 Azure ARM 模板(下面的代码片段)执行以下 powershell 脚本,

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

并出现以下错误。

{
    "status": "failed",
    "error": {
        "code": "DeploymentScriptError",
        "message": "The provided script failed with the following error:\r\nSystem.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()\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/removeappregistrationurl.ps1: line 33\r\nat <ScriptBlock>, <No file>: line 1\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 295. Please refer to https://aka.ms/DeploymentScriptsTroubleshoot for more deployment script information."
    }
}

ARM 模板片段

{
    "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"
    }
}

我可以从本地 powershell 实例和 Azure 云 shell 运行命令,没有任何问题。

最佳答案

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.

该错误消息表明您的计算机上未安装 Connect-AzureAD。如果 AzureAD 模块未正确导入。

您可以按照以下步骤对 Azure AD 模块进行故障排除。

  1. 您可以通过运行以下命令来验证 AzureAD 模块是否已正确安装。

    获取安装模块 | Where-Object {$_.Name -match "AzureAD"}

enter image description here

您还可以使用以下命令验证系统上是否存在 Connect-AzureAD cmdlet。

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

enter image description here

  • 如果该模块未列出,则需要使用以下命令安装它

    安装模块-名称 AzureAD

  • 确保输入“A” – 全部同意来确认安装。

    enter image description here

  • 安装模块后,请确保使用以下命令导入它

    导入模块-名称 AzureAD

  • 使用以下命令更新 AzureAD 模块

    `Update-module AzureAd`
    
  • 更新 AzureAD 模块后,请尝试运行 Connect-AzureAD

  • 关于azure - 通过 ARM 模板运行 powershell 命令会抛出 "The term ' Connect-AzureAD' 未被识别为 cmdlet 的名称”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75855812/

    相关文章:

    Azure API 管理 REST API 调用突然失败

    azure - 为 Azure CDN 创建服务主体失败,并出现租户权限错误

    string - Powershell文件和目录名称为字符串,无装饰

    powershell - 如何检测Azure Add-AzureAccount登录是否失败或被取消?

    unit-testing - 带和不带 ParameterFilter 的模拟 Get-ADUser

    azure - 如何将 AAD 用于 Azure 版本 "Windows Authentication"从 Web 应用程序到 Web API 应用程序?

    c# - Azure - Blob 容器上的列表操作的排序顺序

    azure - 由于“拒绝公共(public)网络访问”设置为"is",连接被拒绝

    python - 尝试使用 azure-graphrbac 创建 Azure B2C 用户

    Azure虚拟机: the user account used to connect to remote PC did not work