Script to get MS Team user information

You will come across requirement where you need to fetch users MS Teams related information e.g. you are going to enable voice services for the users, so you need to get the required information whether user have required license, usage location, TeamsUpgradeEffectiveMode etc.. So I have written script to get all these MS Teams user related information.

Save the below file as .ps1. Run the script. It will ask for input file. Please specify input file that contain userprincipalname. Once script complete it will create output csv file with domainname of one of the userprincipal name. Output file will named as domainname+Current date and time.csv.

<# 
Header of the input file would contain only one column with name as userprincipalname
connect to both teams and msolservice
connect-microsoftteams
connect-msolservice
 #>

$users = $null
$source = $null
$destination = $null
$error.clear()
$Results = @()

while ($source -eq $null){
$source = read-host "Enter source file name"
if (-not(test-path $source)){
    Write-host "Invalid file path, re-enter."
    $source = $null
    }
if ((get-item $source).psiscontainer){
    Write-host "Source must be a file, re-enter."
    $source = $null
    }
elseif($source -notmatch "(\.csv)"){
	Write-host "The file must be a .csv"
	$source = $null
	}
}

$users = import-csv $source
#$users = get-csonlineuser   <# Uncomment this line and comment above line if you want report for all Csonline users.

$domain=$users[0].userprincipalname <#fetch the first vaule of userprincipalname column#>
$Before = $domain.Split("@")[1]      # This will remove everything before @ from $domain variable value
$destination=$before+(Get-Date -Format "dd-MM-yyyy__HH_mm_ss")+".csv"   # This will generate destination file name with current date and time.#>

$count = $users.count
write-host "Processing " $count "Users...Please wait..." -foregroundcolor Yellow -backgroundcolor Black


ForEach ($user in $users) {

 	Write-host "Processing user " -NoNewline
	Write-host $user.userprincipalname -BackgroundColor Red 

	Try{
				
	  $csonlineusers=get-csonlineuser -identity $user.userprincipalname 
	  $msolusers= Get-MsolUser -SearchString $user.userprincipalname
      
      $Properties = @{
      Name = $csonlineusers.DisplayName
      userprincipalname = $csonlineusers.UserPrincipalName
      UsageLocation = $msolusers.usagelocation
      License = $msolusers.Licenses
      HostingProvider = $csonlineusers.HostingProvider
      SipAddress = $csonlineusers.SipAddress
      EnterpriseVoiceEnabled = $csonlineusers.EnterpriseVoiceEnabled
      country = $csonlineusers.country
      LineURI = $csonlineusers.LineURI
      HostedVoiceMail = $csonlineusers.HostedVoiceMail
      OnlineVoiceRoutingPolicy = $csonlineusers.OnlineVoiceRoutingPolicy
      TeamsCallingPolicy = $csonlineusers.TeamsCallingPolicy
      TenantDialPlan = $csonlineusers.TenantDialPlan
      TeamsUpgradeEffectiveMode = $csonlineusers.TeamsUpgradeEffectiveMode 
      TeamsUpgradePolicy = $csonlineusers.TeamsUpgradePolicy 
      CallingLineIdentity = $csonlineusers.CallingLineIdentity
      TeamsIpPhonePolicy = $csonlineusers.TeamsIpPhonePolicy
      TeamsCarrierEmergencyCallRoutingPolicy = $csonlineusers.TeamsCarrierEmergencyCallRoutingPolicy
      AssignedPlans = $csonlineusers.AssignedPlan -join ','
      }
		
$Results += New-Object psobject -Property $properties

$Results | Select-Object Name,userprincipalname,UsageLocation,License,AssignedPlans,HostingProvider,OnPremHostingProvider, SipAddress, EnterpriseVoiceEnabled, country, LineURI, HostedVoiceMail, OnlineVoiceRoutingPolicy, TeamsCallingPolicy, TenantDialPlan, TeamsUpgradeEffectiveMode, TeamsUpgradePolicy, CallingLineIdentity, TeamsIpPhonePolicy, TeamsCarrierEmergencyCallRoutingPolicy | Export-Csv -notypeinformation -Path $destination

		}
	Catch{
	$user.userprincipalname  | Out-File C:\scripts\ProvisioningErrors.log -Append
	$_.Exception | Out-File C:\scripts\ProvisioningErrors.log -Append
	}
	
}

start $destination

write-host "there where " + $error.Count + "errors in this batch"
$error.clear()

sample input file:

This entry was posted in MS Teams, Powershell. Bookmark the permalink.

Leave a Reply

Your email address will not be published.