【发布时间】:2016-12-15 12:14:27
【问题描述】:
我对 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 秒,但这似乎没有任何区别。 我是否必须“重新启动”脚本或其他东西才能找到用户,还是有其他方法可以解决这个问题?
提前致谢!
【问题讨论】:
标签: powershell automation office365