FroggyFreshh
FroggyFreshh

Reputation: 81

Office 365 Online Sync, Can't find user

I'm pretty new to powershell (2 months experience) and i'm creating my own scripts to automate some tasks.

Now i'm working on a usercreation script. Everything works fine until I arrive at the office365 part.

<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

Now this last part is where I get the following error:

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

Meaning the user doesn't exist in Office 365 even tho I forced synced and issued a wait 30seconds (just to be sure its synced up)

If I try to find the user after the script returns this error I CAN find it ...

Get-MsolUser -UserPrincipalName $email

I have tried upping the wait time to 60s but that doesn't seem to make any diffrence. Do I have to "restart" the script or something to find the user or is there any other way I can go about this ?

Thanks in Advance!

Upvotes: 0

Views: 957

Answers (1)

4c74356b41
4c74356b41

Reputation: 72191

No, you don't have to restart the script or do anything fancy at all, just increase the Sleep interval or check if the Start-ADSyncSyncCycle has a -Wait parameter (which it should not) or try writing something like:

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

Upvotes: 1

Related Questions