I'm working on setting up business rule provisioning for Lync and UM. I've cobbled together a couple of scripts which should work by looking at them, but do not always work when initiated through the after user creation business rule. However, the same scripts work when executing manually through admin interface or website.
The first try/catch block will usually enable the user for Lync. However, the second block with Set-CsUser fails when business rule initiated with the outpur error referencing not being able to find the user. Using UPN or sip address.
Any help will be greatly appreciated.
$lyncServer = "LYNCSERVER.com"
$sessionOptions = New-PSSessionOption -SkipRevocationCheck -SkipCACheck -SkipCNCheck
$session = New-PSSession -ConnectionUri https://$lyncServer/ocspowershell -SessionOption $sessionOptions -Authentication NegotiateWithImplicitCredential
Import-PSSession -session $session -AllowClobber
$registrarPool = "POOL.LYNCSERVER.com"
$officeNumber = "%telephoneNumber%" -ireplace '.*([2-9]\d{2})(\D*)([2-9]\d{2})(\D*)(\d{4}).*', '+1$1$3$5'
$ext = $officeNumber.substring($officeNumber.length - 5, 5)
$StopLoop = $False
do{
try{
Enable-CsUser -Identity "%userPrincipalName%" -RegistrarPool $registrarPool -SipAddressType SamAccountName -SipDomain SIPDOMAIN.com
$StopLoop = $True
}
catch{
Write-Host "Wait for user sync"
Start-Sleep -s 60
}
}
While ($StopLoop -eq $False)
#wait a second
Start-Sleep -s 4
$StopLoop = $False
do{
try{
Set-CsUser -Identity "%userPrincipalName%" -EnterpriseVoiceEnabled $True -LineUri "tel:$officeNumber;ext=$ext" -PassThru | Set-CsClientPin -Pin #######
$StopLoop = $True
}
catch{
Write-Host "Wait for user sync"
Start-Sleep -s 60
}
}
While ($StopLoop -eq $False)
Remove-PSSession -Session $session