Ce script permet d’importer les numéros de téléphone des utilisateurs dans AD.
L’attribut “OfficePhone” de l’utilisateur AD est utilisé.
Le fichier CSV est au format :
Tel,Account
0412345678,User001
0412345678,User002
0412345678,User003
0412345678,User004
Note : le fichier $LogFile permet l’historisation des modifications et le retour arrière.
Import-Module ActiveDirectory
$fileImport= ‘C:\TEMP\tel.csv’
$LogFile= “c:\temp\LOG_PhoneNumHistory_$(get-date -Format “yyyyMMddHHmmss”).csv”
$Res=@()
$lst=Import-Csv -Path $fileImport -Delimiter “;”
foreach($enr in $lst){
Write-Host $enr.Account ‘ –> ‘ $enr.tel
$userAD=””
$userAD=Get-ADUser -Identity $enr.Account -Properties OfficePhone
if($userAD -eq “”){
$DataCollect = New-Object System.object
$DataCollect | Add-Member -name ‘FILESAMAccountName’ -MemberType NoteProperty -Value $enr.Account
$DataCollect | Add-Member -name ‘ADSAMAccountName’ -MemberType NoteProperty -Value “Not in directory”
$DataCollect | Add-Member -name ‘OfficePhoneOLD’ -MemberType NoteProperty -Value “”
$DataCollect | Add-Member -name ‘OfficePhoneNEW’ -MemberType NoteProperty -Value “”
}else{
$userOfficeNEW=$(“{0:+33 # ## ## ## ##}” -f [int]$enr.tel)
$DataCollect = New-Object System.object
$DataCollect | Add-Member -name ‘FILESAMAccountName’ -MemberType NoteProperty -Value $enr.Account
$DataCollect | Add-Member -name ‘ADSAMAccountName’ -MemberType NoteProperty -Value $userAD.SamAccountName
$DataCollect | Add-Member -name ‘OfficePhoneOLD’ -MemberType NoteProperty -Value $userAD.OfficePhone
$DataCollect | Add-Member -name ‘OfficePhoneNEW’ -MemberType NoteProperty -Value $userOfficeNEW
$userAD | Set-ADUser -OfficePhone $userOfficeNEW
}
$Res+=$DataCollect
}
$Res | Export-Csv $LogFile -Encoding UTF8 -Delimiter “;”
Il est possible d’ajuster le formatage du numéro de téléphone :
[int]$a = 0412345678
“{0:+33 (0) # ## ## ## ##}” -f $a
“{0:0# ## ## ## ##}” -f $a
Pour supprimer le numéro de téléphone :
Get-ADUser -Identity $enr.Account -Properties OfficePhone | Set-ADUser -OfficePhone $null