beeing a newbie in programming and powershell scripting, i hope that you can help me out with the following problem.
Currently iam working on a script to make it easy to assign a telephone-number to a AD-User. The idea is just to enter the name of the user and the script searches for the UPN and assigns the correct phonennumber to use in teams (pbx) . In the end the script should display the status of the team user to check if the number was configured correctly via "Get-CsOnlineUser -Identity $aduserupn"
The Script looks like this:
Write-Host "Teams Script V1.4" -ForegroundColor Yellow -BackgroundColor DarkGreen
Connect-MicrosoftTeams
# Get user and store AD-Phonennumber
function step1 {
$done = $False
while (-not $done){
try
{
$aduser = $null
$VoiceRoutingPolicy = 'Via-Provider-Policy'
$decission = ''
$user = Read-Host -Prompt 'Please enter the user.name'
$aduser = Get-ADUser -Identity $user -Properties telephonenumber -ErrorAction Stop
$extension = $aduser.telephonenumber -replace '\s',''
$aduserupn = $aduser.UserPrincipalName
Grant-CsOnlineVoiceRoutingPolicy -Identity $aduser.UserPrincipalName -PolicyName $VoiceRoutingPolicy -ErrorAction Stop
Set-CsUser -Identity $aduser.UserPrincipalName -EnterpriseVoiceEnabled $true -HostedVoiceMail $false -OnPremLineURI tel:$extension -ErrorAction Stop
write-host $aduser.UserPrincipalName
Get-CsOnlineUser -Identity $aduserupn -ErrorAction Stop | select DisplayName, RegistrarPool, OnlineVoiceRoutingPolicy, LineURI, OnPremLineURI, EnterpriseVoiceEnabled, HostedVoicemail
write-host "Done - Please check status above" -ForegroundColor Yellow -BackgroundColor Green
$decission = Read-Host -Prompt 'Do you want to activate another user? (y)es / (n)no'
if ($decission -eq 'y'){
} else {
$done = $True
}
}
catch
{
if ($aduser -eq $null) {
Write-Host "The Username was not found, please try again!" -ForegroundColor Red -BackgroundColor DarkGreen
}
}
}
}
step1
write-host "Done - Disconnecting Session." -ForegroundColor Yellow -BackgroundColor Green
get-pssession | remove-pssession
The problem is, that this Get-CsOnlineUser cmdlet used with the script does not show me any result. When i directly enter it into powershell, it works and shows me the result:
DisplayName : Name, Name
RegistrarPool : sippoolAM41E07.infra.lync.com
OnlineVoiceRoutingPolicy : Provider-Policy
LineURI : tel:+XXXXXXXXXXXXX
OnPremLineURI : tel:+XXXXXXXXXXXXX
EnterpriseVoiceEnabled : True
HostedVoiceMail : False
All others csOnline-cmdlets are working fine. Do you see whats wrong here?
Thank you very much!
Copying answer from @panagiotis comments for better understanding.
DON'T use the script tag. Every SO question is about scripts one way or another. It's no better than adding code as a tag. That tag is just noise, it was deleted in the past and will be deleted very soon again.