3 February 2013

Import Lync Users from CSV

Import Lync Users from CSV.

NOTE

In the CSV the following headers need to be present:-


Column Header
Description
ADUsername
Valid AD User Name
Pool
The Lync Pool the user will be assigned to
LineUri
This must be in the format tel:+xxxxx
ClientPolicy
Get-CsClientPolicy to see what’s configured or set to add new PS only
ConferencePolicy
Get-CsConferencingPolicy to see, add from Control Panel or PS
DialPlanpolicy
Get-CsDialPlan to see, add from Control Panel or PS
Voicepolicy
Get-CsVoicePolicy to see, add from Control Panel or PS
ExternalAccessPolicy
Get-CsExternalAccessPolicy to see, add from Control Panel or PS


########################################################################
# Author: Paul B
# Purpose: Script to Import Lync Users from CSV
# Version: 1.0
# 02/02/2013 - Release 1.0
#Change
# Instructions
# Edit the below variables to match your requirements
#
########################################################################

#Variables

$File = "C:\Lync\Scripts\importusers\LyncUsers.csv"
$Log = New-Item -ItemType File -Path "C:\Lync\Scripts\importusers\userlog.txt" -Force

#Import csv

$usercsv = Import-Csv -path $file -Delimiter ','

#Check if user file is empty.

if ($Usercsv -eq $null)
{
 write-host "No Users Found in Input File"
 exit 0
}


#Get total number of users in CSV file and begin processing.


$count = $Usercsv | Measure-Object | Select-Object -expand count


Write-Host "Found " $count "Users to import."

Write-Host "Processing Users.....`n"
$index = 1

ForEach ($user in $usercsv)


{


Write-Host "Processing User " $index " of " $count

$Fullname = $User.ADUsername
$aduser = get-csaduser -Identity $Fullname

#Check if user is in AD.  Log if they are NOT.

if ($aduser -eq $null) {
$notinad = $true
Write-Host "User " $Fullname " is not in AD.  Double check spelling, etc." -Foregroundcolor Red
Add-Content -Path $Log -Value "$($Fullname) is not in AD.  Double check spelling, etc."
}

else {

$notinad = $false
}

#If user is in AD check if enabled in Lync and log if enabled.

if ($aduser.Enabled) {
Write-Host $User.ADUsername"is already enabled in Lync, skipping."  -Foregroundcolor Yellow
Add-Content -Path $Log -Value "$($Fullname) is already enabled in Lync."
}

#User not enabled in Lync

else {
Write-Host "Adding user " $User.ADUsername -Foregroundcolor Green
                Enable-CsUser -Identity $User.ADUsername -RegistrarPool $user.Pool -SipAddress $user.sipaddresstype emailaddress

#Check if last command failed.  If it does, log it.

if(!$?) {
Add-Content -Path $Log -Value "$($Fullname) not enabled.  $(Get-Date)$($error[0])"
continue
}
#Enable for EV and Add Line URI
Set-csuser -identity $User.ADUsername -enterprisevoiceenabled $true -lineuri $user.LineURI
#Check if last command failed.  If it does, log it.
if(!$?) {
Add-Content -Path $Log -Value "$($Fullname) not enabled.  $(Get-Date)$($error[0])"
continue
}
#CSClientPolicy
Grant-csclientpolicy -Identity $User.ADUsername -PolicyName $user.clientpolicy
#Check if last command failed.  If it does, log it.
if(!$?) {
Add-Content -Path $Log -Value "$($Fullname) not enabled.  $(Get-Date)$($error[0])"
continue
}
#Conferencing Policy
Grant-csconferencingpolicy -Identity $User.ADUsername -PolicyName $user.conferencepolicy
#Check if last command failed.  If it does, log it.
if(!$?) {
Add-Content -Path $Log -Value "$($Fullname) not enabled.  $(Get-Date)$($error[0])"
continue
}
#Dial Plan
Grant-CsDialPlan -Identity $User.ADUsername -PolicyName $user.DialPlanpolicy
#Check if last command failed.  If it does, log it.
if(!$?) {
Add-Content -Path $Log -Value "$($Fullname) not enabled.  $(Get-Date)$($error[0])"
continue
}
#Voice Policy
Grant-CsVoicePolicy -Identity $User.ADUsername -PolicyName $user.Voicepolicy
#Check if last command failed.  If it does, log it.
if(!$?) {
Add-Content -Path $Log -Value "$($Fullname) not enabled.  $(Get-Date)$($error[0])"
continue
}
#External Access Policy
grant-csexternalaccesspolicy -Identity $User.ADUsername -PolicyName $user.externalaccesspolicy
#Check if last command failed.  If it does, log it.
if(!$?) {
Add-Content -Path $Log -Value "$($Fullname) not enabled.  $(Get-Date)$($error[0])"
continue
}
    }
$index++
}

Write-Host "Importing of Lync users from CSV completed!"

Write-Host ""
Write-Host ""
Write-Host ""

update-csuserdatabase -verbose

sleep -seconds 300
update-csaddressbook -verbose




No comments:

Post a Comment