带有 Get-ADObject -LDAPFilter 的 Powershell 脚本在任务计划程序中启动时出错
Powershell script with Get-ADObject -LDAPFilter give error when launched in task scheduler
脚本在 powershell 控制台中运行,但在 运行 作为计划任务时出错。
错误:
> 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
这是一行:
if ((Get-ADObject -LDAPFilter "(&(GivenName=$PRENOM)(Sn=$NOM))" | ft -HideTableHeaders DistinguishedName | Out-String).Trim())
编辑:
剧本的一部分。我无法完整上传
$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()
}
}
编辑2:
NOM;PRENOM;PLACE;;1028;
NOM;PRENOM;PLACE;0x-xx-xx-xx-xx;;
由于您在输入的 CSV 文件中使用重音字符,因此将其保存为 UTF8 编码非常重要。
此外,脚本本身应该是 UTF8 格式,并查看显示 caractŠre
的错误消息,这里不是这种情况..
确保两个文件都采用 utf8 编码后,下面的代码应该可以工作:
$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'.."
}
}
脚本在 powershell 控制台中运行,但在 运行 作为计划任务时出错。
错误:
> 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
这是一行:
if ((Get-ADObject -LDAPFilter "(&(GivenName=$PRENOM)(Sn=$NOM))" | ft -HideTableHeaders DistinguishedName | Out-String).Trim())
编辑: 剧本的一部分。我无法完整上传
$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()
}
}
编辑2:
NOM;PRENOM;PLACE;;1028;
NOM;PRENOM;PLACE;0x-xx-xx-xx-xx;;
由于您在输入的 CSV 文件中使用重音字符,因此将其保存为 UTF8 编码非常重要。
此外,脚本本身应该是 UTF8 格式,并查看显示 caractŠre
的错误消息,这里不是这种情况..
确保两个文件都采用 utf8 编码后,下面的代码应该可以工作:
$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'.."
}
}