【发布时间】: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