我使用连接到 AzureAD 的 powershell 创建了一个脚本,并且使用以下命令运行良好:
Connect-AzureAD -TenantId $TenantId
弹出登录窗口,我可以成功连接。但是,我需要将脚本设置为自动运行,所以我找到了这段代码:
# 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
但我似乎无法让它工作。我使用以下内容生成我的 cred.txt 文件:
Read-Host -assecurestring | convertfrom-securestring | out-file C:\cred.txt
当我运行它时,我收到以下错误:
Connect-AzureAD : 发生一个或多个错误。: ContentType 中提供的字符集无效。无法使用无效字符集将内容读取为字符串。在 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:出现一个或多个错误。在 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 : ContentType 中提供的字符集无效。无法使用无效字符集将内容读取为字符串。在 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"' 不是受支持的编码名称。有关定义自定义编码的信息,请参阅 Encoding.RegisterProvider 方法的文档。参数名称:name At line:9 char:1 + Connect-AzureAD -TenantId $TenantId -Credential $AzureCred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : AuthenticationError: (:) [Connect-AzureAD], ArgumentException + FullyQualifiedErrorId :连接 AzureAD、Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD
Connect-AzureAD : 发生一个或多个错误。: ContentType 中提供的字符集无效。无法使用无效字符集将内容读取为字符串。在 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
我使用相同的脚本登录了不同的租户,它工作正常。它只是不适用于拥有联邦的租户。除了关闭联合之外,还有什么解决方法吗?