【问题标题】:Using PowerShell to update AD users from CSV file使用 PowerShell 从 CSV 文件更新 AD 用户
【发布时间】:2016-09-01 12:42:13
【问题描述】:

我正在使用 PowerShell 脚本向 AD 中的所有用户添加一些信息。出于某种原因,如果用户的名字中有撇号(例如 Tim O'Reilly),我会不断收到错误消息。

如何格式化脚本,使其包含带撇号的名称?

我的脚本:

# Import AD Module
Import-Module ActiveDirectory

write-Host 'Starting to update AD Attributes.......' -NoNewline -ForegroundColor Yellow
# Import CSV into variable $users

$users = Import-Csv -Path C:\Scripts\users.csv

# Loop through CSV and update users if the exist in CVS file

foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter "displayName -eq '$($user.Name)'" -Properties * -SearchBase "DC=My,DC=domain,DC=com" |
Set-ADUser -Company $($user.Email)
}

Write-Host 'done!' -ForegroundColor Green

这是我收到的错误消息:

Get-ADUser:解析查询时出错:'displayName -eq 'Tim O'Reilly'' 错误消息:“语法错误”在位置:“29”。在 C:\Scripts\Update-information\Update-Users-2.ps1:13 char:1 + Get-ADUser -Filter "displayName -eq '$($user.Name)'" -Properties * -S ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr osoft.ActiveDirectory.Management.Commands.GetADUser

如果我能在这里得到任何帮助,我将不胜感激。

谢谢,

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    您可以在 csv 文件中使用一些自定义分隔符,而不是修改脚本。只需使用自定义字符(如“:”(Tim : O'Reilly))划分您的数据,并为 import-csv cmdlet 添加分隔符开关,如

    $users = Import-Csv -Path C:\Scripts\users.csv -Delimiter :
    

    【讨论】:

    • 应该像 -Delimiter " ' " (我必须添加空格使其可见)?
    • 这是我现在收到的错误消息:Get-ADUser : The search filter cannot be recognized
    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 2015-06-15
    • 2015-08-10
    • 1970-01-01
    • 2021-12-23
    • 2023-02-01
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多