如何使用具有自动凭据的connect-msolservice?

时间:2019-12-23 20:57:15

标签: powershell office365 exchange-server mfa multi-factor-authentication

顶部代码块不起作用,而底部代码块起作用。底部提示出现典型的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 auth window that I want to avoid

0 个答案:

没有答案