Powershell Connect-AzureAD错误“ accessing_ws_metadata_exchange_failed”

时间:2019-02-07 21:52:40

标签: azure powershell azure-active-directory azure-powershell

我正在编写Powershell脚本,需要在其中使用代码连接到Azure Active Directory。

如果我通过提示符进行连接,则可以正常工作,但是使用代码(在代码中提供用户ID和密码)会引发以下错误:

  

Connect-AzureAD:发生一个或多个错误。:   accessing_ws_metadata_exchange_failed:正在访问WS元数据交换   在C:\ Users \ yawer.iqbal \ Desktop \ Untitled3.ps1:8 char:3失败   + Connect-AzureAD -Credential $ Credential-TenantID $ tenant

这是我的代码:

$User = "myid@outlook.com"
$PWord = ConvertTo-SecureString -String "*******" -AsPlainText -Force
$tenant = "bingu12outlook.onmicrosoft.com"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord
  Import-Module AzureAD
  Connect-AzureAD -Credential $Credential -TenantID $tenant

我尝试过: 添加此行,但它也不起作用:

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

请帮助

1 个答案:

答案 0 :(得分:0)

不确定是什么原因导致的问题,如果可以接受,您可以使用工作帐户来完成,这对我来说很好。

首先,导航至天蓝色门户中的Azure Active Directory-> Users-> New user,请参阅此link。然后,您将获得一个xxx@bingu12outlook.onmicrosoft.com之类的工作帐户,请记住在首次登录时重设密码。

我的特定测试命令:

$User = "joyxx@xxxx.onmicrosoft.com"
$PWord = ConvertTo-SecureString -String "xxxxxxx" -AsPlainText -Force
$tenant = "xxxx.onmicrosoft.com"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User,$PWord
Connect-AzureAD -Credential $Credential -TenantID $tenant

enter image description here