【问题标题】:Azure commands not working in ISE but do in PS shellAzure 命令在 ISE 中不起作用,但在 PS shell 中起作用
【发布时间】:2019-10-03 16:36:42
【问题描述】:

我正在尝试使用我的公司 O365 帐户连接到我的 Azure AD。当我在 PS shell 中运行命令时,效果很好。但尝试制作 ps1 脚本文件时,ISE 中的命令失败。下面的代码和错误:

代码:

$managedcred = get-storedcredential -Target o365

connect-azuread -credential $managedcred

错误:

Connect-AzureAD:出现一个或多个错误。:AADSTS50126:用户名或密码无效。 跟踪 ID:3bbf3cba-61c3-45c5-a19f-60973b7c2700 相关 ID:14599060-8bb3-4fce-afda-621efc3660ed 时间戳:2019-10-03 16:05:03Z 在行:1 字符:1 + 连接 AzureAD -Credential $managedcred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], AadAuthenticationFailedException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD


Connect-AzureAD:出现一个或多个错误。 在行:1 字符:1 + 连接 AzureAD -Credential $managedcred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], AggregateException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD


Connect-AzureAD:AADSTS50126:用户名或密码无效。 跟踪 ID:3bbf3cba-61c3-45c5-a19f-60973b7c2700 相关 ID:14599060-8bb3-4fce-afda-621efc3660ed 时间戳:2019-10-03 16:05:03Z 在行:1 字符:1 + 连接 AzureAD -Credential $managedcred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], AdalServiceException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD


Connect-AzureAD:响应状态代码不表示成功:400 (BadRequest)。 在行:1 字符:1 + 连接 AzureAD -Credential $managedcred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], HttpRequestException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD


Connect-AzureAD : {"error":"invalid_grant","error_description":"AADSTS50126: 用户名或密码无效。\r\n跟踪 ID: 3bbf3cba-61c3-45c5-a19f-60973b7c2700\r\n相关 ID: 14599060-8bb3-4fce-afda-621efc3660ed\r\n时间戳: 2019-10-03 16:05:03Z","error_codes":[50126],"timestamp":"2019-10-03 16:05:03Z","trace_id":"3bbf3cba-61c3-45c5-a19f-60973b7c2700","correlation_id ":"14599060-8bb3-4fce-afda-621efc366 0ed","error_uri":"https://login.microsoftonline.com/error?code=50126"}: 未知错误 在行:1 字符:1 + 连接 AzureAD -Credential $managedcred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], AdalException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD


Connect-AzureAD:出现一个或多个错误。:AADSTS50126:用户名或密码无效。 跟踪 ID:3bbf3cba-61c3-45c5-a19f-60973b7c2700 相关 ID:14599060-8bb3-4fce-afda-621efc3660ed 时间戳:2019-10-03 16:05:03Z 在行:1 字符:1 + 连接 AzureAD -Credential $managedcred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Connect-AzureAD], AadAuthenticationFailedException + FullyQualifiedErrorId : Microsoft.Open.Azure.AD.CommonLibrary.AadAuthenticationFailedException,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD

所以看起来第一个命令工作正常,但连接失败。

【问题讨论】:

  • 看起来您用于连接的凭据不正确?你有什么方法可以确认你检索的内容是正确的吗?
  • 我不知道如何确保它们被正确拉动。但是,我在 ISE 和 CLI 中使用了完全相同的两个命令。 CLI 工作,ISE 失败
  • 更新:现在它在任何一个平台、ISE 或 CLI 中都不起作用

标签: powershell azure-active-directory


【解决方案1】:

根据错误消息,您未使用正确的用户名和密码连接 Azure AD。请使用以下命令进行检查。

 Get-StoredCredential -Target O365 -AsCredentialObject

此外,据我了解,您想在没有提示的情况下连接 Azure AD。您也可以使用以下命令:

$name = ""
$password = ""
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($name, $secpasswd)
Connect-AzureAD -Credential $mycreds 

更新

根据研究,应用密码用于与不支持现代身份验证的客户端(例如 Outlook)完成 MFA。更多详情请参考https://support.microsoft.com/en-au/help/12409/microsoft-account-app-passwords-and-two-step-verification。所以我们不能在 PowerShell ISE 中使用应用密码连接 Azure AD。

根据情况,我建议你使用服务主体连接Azure AD。更多详情请参考https://docs.microsoft.com/en-us/powershell/azure/active-directory/signing-in-service-principal?view=azureadps-2.0

【讨论】:

  • 我执行了您提供的命令进行检查,凭据看起来正确。我正在使用“应用程序密码”,因为我的帐户上有两个因素身份验证。硬编码部分给出了相同的错误,错误的用户和通过
  • @TheMattMan0425 您还有其他顾虑吗?如果您没有其他顾虑,您能接受答案吗?它可能会帮助更多的人。
  • 我想我接受了。我接受这不会像我希望的那样工作。
猜你喜欢
  • 1970-01-01
  • 2019-07-31
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多