I m writing a Powershell script where I need to connect to Azure Active Directory using code.
If I connect through prompt it works fine but using code (providing user id and password in code) it throws the following error:
Connect-AzureAD : One or more errors occurred.: accessing_ws_metadata_exchange_failed: Accessing WS metadata exchange failed At C:\Users\yawer.iqbal\Desktop\Untitled3.ps1:8 char:3 + Connect-AzureAD -Credential $Credential -TenantID $tenant
Here is my code:
$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
I have tried: Adding this line but it do not work either:
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
Please help