Office 365 Online Sync,找不到用户

时间:2016-12-15 12:14:27

标签: powershell automation office365

我对powershell(2个月的经验)很陌生,我正在创建自己的脚本来自动执行某些任务。

现在我正在开发一个用户创建脚本。一切正常,直到我到达office365部分。

<Creating AD user based on template and filled info>
<Creating Exchange emailbox>
...
#Retrieving email to specify user
$email = (Get-ADUser $initials -Properties mail).mail

#Remoting into server that has Azure AD Sync installed and forcing sync
Invoke-Command -ComputerName <servername> -ScriptBlock {Start-ADSyncSyncCycle -PolicyType Delta} -Credential $AdminCredentials

#Waiting until sync is done, 30 seconds for testing purposes, can be lowered after some testing 
Write-host "Waiting untill sync is done"
Start-Sleep -s 30

#Connecting to office365
Connect-MsolService -Credential $OfficeCredentials

#Creating Licence withouth exchange
$NoExchange = New-MsolLicenseOptions -AccountSkuId syndication-account:O365_BUSINESS_PREMIUM -DisabledPlans "EXCHANGE_S_STANDARD"

#applying licence to user
Set-MsolUserLicense -UserPrincipalName $email -LicenseOptions $NoExchange

现在最后一部分是我收到以下错误的地方:

Set-MsolUserLicense : User Not Found.  User: .
At <path>\UserCreation.ps1:145 char:1
+ Set-MsolUserLicense -UserPrincipalName $email -LicenseOptions $NoExchange
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.SetUserLicense

意味着用户在Office 365中不存在,即使我强制同步并发出等待30秒(只是为了确保其已同步)

如果我在脚本返回此错误后尝试找到用户,我可以找到它......

Get-MsolUser -UserPrincipalName $email

我已经尝试将等待时间提高到60秒,但这似乎没有任何差异。 我是否必须“重新启动”脚本或其他东西才能找到用户,还是有其他方法可以解决这个问题?

提前致谢!

1 个答案:

答案 0 :(得分:1)

不,您不必重新启动脚本或执行任何花哨的操作,只需增加Sleep间隔或检查Start-ADSyncSyncCycle是否有-Wait参数(它是不应该)或尝试写一些类似的东西:

While ((Get-ADSyncConnectorRunStatus).Runstate -eq "Busy") {
    Start-Sleep 10
}