首页 > 解决方案 > 如何使用带有自动凭据的 connect-msolservice?

问题描述

顶部代码块不起作用,底部代码块起作用。底部提示典型的 MFA O365 登录窗口“Microsoft 登录电子邮件或电话”(如下图所示),我想使用 cmdlet Connect-MsolService 的凭据参数。我们在这里不使用密码,而是使用证书,因此我想以编程方式将用户电子邮件地址提供给 Connect-MsolService cmdlet,并且不使用密码,就像使用 MFA 登录完成身份验证一样窗户。如何实现?

$adminName = "blah@blah.com";
#$pass = ConvertTo-SecureString "notneeded" -AsPlainText -Force;

$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $adminName;
Connect-MsolService -Credential $creds; 
$currentDateTime = (GET-DATE);

$lastDirSyncTime =(Get-MsolCompanyInformation).LastDirSyncTime.ToLocalTime();

$timeDifference = $currentDateTime - $lastDirSyncTime;

Write-Host "";
Write-Host "Minutes since the last sync:" $timeDifference.TotalMinutes "minutes."; 

下面的工作,但打开如下图所示的 MFA 身份验证窗口,要求输入登录用户名

Connect-MsolService; 
$currentDateTime = (GET-DATE);

$lastDirSyncTime =(Get-MsolCompanyInformation).LastDirSyncTime.ToLocalTime();

$timeDifference = $currentDateTime - $lastDirSyncTime;

Write-Host "";
Write-Host "Minutes since the last sync:" $timeDifference.TotalMinutes "minutes."; 

我想避免的 O365 身份验证窗口

标签: powershelloffice365exchange-servermulti-factor-authentication

解决方案


推荐阅读