HI all,
I need your help.
I've a powershell script that work so fine and that update the "pager" attribut for every user found in temp.csv file.
#########################################################
#Import-module ActiveDirectory
FUNCTION Import-PagerData
{
Param (
[String]$File
)
# Read the contents of the text file into memory.
[System.Collections.ArrayList]$Data = Get-Content $File
# Dynamic array to hold the strings once filtered.
$Array = @()
# Loop through each line andfilter it.
ForEach ($D in $Data)
{
# Do not process the line with the "---"
If($D -NotLike "*--------*")
{
# Replace all spaces from the string that have 2 side-by-side
# spaces with a comma
While ($D -match "\s\s+")
{
$DSpace = $Matches.Values
$D = $D.Replace($DSpace,",")
}
# Replace all commas from the string that have 2 side-by-side
# commas with a single comma
While ($D -match ",,")
{
$DSpace = $Matches.Values
$D = $D.Replace($DSpace,",")
}
#Clean up random diviations.
If ($D.LastIndexOf(","))
{
$d = $D.Remove($d.length-1)
}
$D = $D.replace(", ",",")
$D = $D.replace(" ,","")
$Array += $D
}
}
# Write the data to a temporary file.
$TempPath = "D:\temp\Temp.csv"
$Array | Out-File -filepath $TempPath
# Import the data as a CSV file and modify AD.
Import-Csv -Path $TempPath |
foreach-Object {$LN = "$($_.NOM)" ; $FN = "$($_.PRENOM)";
$Pager = "$($_.{NUMERO_SERIE})";
ldifde -f d:\temp\Tempo.csv -s toto.local -d "dc=toto,dc=local" -p subtree -r "(&(objectCategory=person)(objectClass=User)(sn=$LN)(GivenName=$FN))" -l "name,pager"
Get-Content -Path d:\temp\Tempo.csv | Select-Object -First 1 > D:\temp\PAGER_AD.ldf
Write "changetype: modify" | Out-File D:\temp\PAGER_AD.ldf -Append
Write "replace: pager" | Out-File D:\temp\PAGER_AD.ldf -Append
Write "pager: $Pager" | Out-File D:\temp\PAGER_AD.ldf -Append
Write "-" | Out-File D:\temp\PAGER_AD.ldf -Append
ldifde -i -f D:\temp\PAGER_AD.ldf -s toto.local
Remove-Item "D:\temp\Tempo.csv"
Remove-Item "D:\temp\PAGER_AD.ldf"
}
# Clean up the Temp file.
Remove-Item "D:\temp\Temp.csv"
}
Import-PagerData -File "D:\temp\Start.txt"
##########################################
my objectif is : Before updating the "pager" attribut, i like to check if the user ( if only LN and FN attribut exist ) in the temp.csv exist in the AD or not.
if exist than update the pager
else if not add a column in the temp.csv file to see : user doesn't exist.
thank you so much.