Home > Software engineering >  Powershell script with Get-ADObject -LDAPFilter give error when launched in task scheduler
Powershell script with Get-ADObject -LDAPFilter give error when launched in task scheduler

Time:03-26

Script is working in powershell console but give error when run as scheduled task.

Error :

>         Get-ADObject : Le filtre de recherche n est pas reconnu
>     Au caractŠre \\get.com\netlogon\Powershell\ACTIVE_DIRECTORY\UPDATE_AD_PHONE.ps1:70
> : 7
>           if ((Get-ADObject -LDAPFilter "(&(GivenName=$PRENOM)(Sn=$NOM))" | ...
>                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>           CategoryInfo          : NotSpecified: (:) [Get-ADObject], ADException
>           FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADObject

Here is the line :

if ((Get-ADObject -LDAPFilter "(&(GivenName=$PRENOM)(Sn=$NOM))" | ft -HideTableHeaders DistinguishedName | Out-String).Trim())

EDIT: Part of the script. I can't upload full

$filepath = "c:\liste\Liste telephonique.csv"
# Les champs (on lit le fichier à partir de la ligne 3)
# NOM;Prénom;SITE;"N° Mobile";"Abrégé";"N° Direct";;;;;;;;
$File = Get-Content -Path $filepath | Select-Object -Skip 3 | ConvertFrom-Csv -Delimiter ';'
Foreach($Ligne in $File)
    {
    $NOM = $Ligne."NOM"
    $PRENOM = $Ligne."Prénom"
    
    # On cherche dans AD si le couple NOM/PRENOM nous retourne un DN
    # Si oui on continue, sinon on quitte la ligne.
    if ((Get-ADObject -LDAPFilter "(&(givenName=$PRENOM)(sn=$NOM))" | ft -HideTableHeaders DistinguishedName | Out-String).Trim()) 
        {
        $DN = (Get-ADObject -LDAPFilter "(&(givenName=$PRENOM)(sn=$NOM))" | ft -HideTableHeaders DistinguishedName | Out-String).Trim() 
        }
    }

EDIT2 :

NOM;PRENOM;PLACE;;1028;
NOM;PRENOM;PLACE;0x-xx-xx-xx-xx;;

CodePudding user response:

Since you are using accented characters in the input CSV file, it is vital you save this in UTF8 encoding.
Also the script itself should be in UTF8 and looking at the error message where it says caractŠre, this is not the case here..

After you have made sure both files are in utf8 encoding below code should work:

$filepath = "c:\liste\Liste telephonique.csv"
# Les champs (on lit le fichier à partir de la ligne 3)
# NOM;Prénom;SITE;"N° Mobile";"Abrégé";"N° Direct";;;;;;;;
$File = Import-Csv -Path $filepath -Delimiter ';' -Encoding UTF8
foreach($Ligne in $File) {
    $NOM    = $Ligne."NOM"
    $PRENOM = $Ligne."Prénom"

    # On cherche dans AD si le couple NOM/PRENOM nous retourne un DN
    # Si oui on continue, sinon on quitte la ligne.

    # don't use Format-* cmdlets if you want to process using its properties
    # Format-* cmdlets are for display purposes ONLY
    $adObject = Get-ADObject -LDAPFilter "(&(givenName=$PRENOM)(sn=$NOM))" -ErrorAction SilentlyContinue
    if ($adObject) {
        # proceed with the code once you have found the object
        $DN = $adObject.DistinguishedName

        # using names as search filters however CAN result in multiple objects being found
        # so you may consider processing in another loop like
        # $adObject | ForEach-Object {
        #     $DN = $_.DistinguishedName
        #     # process this object
        # }
    }
    else {
        Write-Warning "Could not find object using NOM:'$NOM'  Prénom: '$PRENOM'.."
    }
}
  • Related