我正在尝试通过 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 模块进行故障排除。
您可以通过运行以下命令来验证 AzureAD 模块是否已正确安装。
获取安装模块 | Where-Object {$_.Name -match "AzureAD"}
您还可以使用以下命令验证系统上是否存在 Connect-AzureAD cmdlet。
Find-Command | Where-Object {$_.Name -match "Connect-AzureAD"}
如果该模块未列出,则需要使用以下命令安装它
安装模块-名称 AzureAD
确保输入“A” – 全部同意来确认安装。
安装模块后,请确保使用以下命令导入它
导入模块-名称 AzureAD
使用以下命令更新 AzureAD 模块
`Update-module AzureAd`
更新 AzureAD 模块后,请尝试运行 Connect-AzureAD。
关于azure - 通过 ARM 模板运行 powershell 命令会抛出 "The term ' Connect-AzureAD' 未被识别为 cmdlet 的名称”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75855812/