Automate Connect-AzureAD Powershell

7.1k Views Asked by At

I created a script using powershell that connect to AzureAD and it was working fine using the following:

Connect-AzureAD -TenantId $TenantId

The sign in window pops up and I am able to successfully connect. However, I need to setup the script to run automatically so I found this code to do it:

# Set Configs
$TenantId = "<tenant id>"
$AzureUser = "<user>" 
$AzurePass = Get-Content "cred.txt" | ConvertTo-SecureString

# Connect to Azure AD
$AzureCred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $AzureUser, $AzurePass
Connect-AzureAD -TenantId $TenantId -Credential $AzureCred

But I can't seem to get it to work. I use the following to generate my cred.txt file:

Read-Host -assecurestring | convertfrom-securestring | out-file C:\cred.txt

When I run it, I get the following error:

Connect-AzureAD : One or more errors occurred.: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set. At line:9 char:1 + Connect-AzureAD -TenantId $TenantId -Credential $AzureCred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], AadAuthenticationFailedException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD
Connect-AzureAD : One or more errors occurred. At line:9 char:1 + Connect-AzureAD -TenantId $TenantId -Credential $AzureCred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], AggregateException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD
Connect-AzureAD : The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set. At line:9 char:1 + Connect-AzureAD -TenantId $TenantId -Credential $AzureCred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], InvalidOperationException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD
Connect-AzureAD : '"utf-8"' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Parameter name: name At line:9 char:1 + Connect-AzureAD -TenantId $TenantId -Credential $AzureCred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], ArgumentException + FullyQualifiedErrorId : Connect-AzureAD,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD
Connect-AzureAD : One or more errors occurred.: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set. At line:9 char:1 + Connect-AzureAD -TenantId $TenantId -Credential $AzureCred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Connect-AzureAD], AadAuthenticationFailedException + FullyQualifiedErrorId : Microsoft.Open.Azure.AD.CommonLibrary.AadAuthenticationFailedException,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD

I logged in on a different tenant using the same script and it was working fine. It is just not working to the tenant that has federation on. Any workaround for this aside from turning off federation?

0

There are 0 best solutions below