【发布时间】:2020-10-07 13:06:07
【问题描述】:
我正在编写一个脚本,该脚本将从 CSV 文件更新 Active Directory 中的用户属性。我现在只有一个错误,它与语法有关。不确定正确的语法是什么;我提供了运行脚本时收到的错误。
这是从 CSV 文件更新 AD 用户的脚本。
#Updates AD user attributes from CSV file
$credential = Get-Credential
#Load data from file.csv
$ADUsers = Import-csv file_location
# Server
$server = "127.0.0.1"
# Count variable for number of users update
$count = 0
#Go through each row that has user data in the CSV we just imported
foreach ($User in $ADUsers)
{
# Read user data from each field in each row and assign to variables.
$Username = $User.Username
$Title = $User.Title
$Office = $User.Office
$Description = $User.Description
#Check to see if the user already exists in AD. If they do, we update.
if (Get-ADUser -Filter "SamAccountName -eq $Username" -Server $server -Credential $credential)
{
#Set User attributes
Set-ADUser -Title $Title -WhatIf `
-Office $Office -WhatIf `
-Description $Description -WhatIf
# Print that the user was updated
Write-Host $Username "- User attributes have been updated." -ForegroundColor Yellow
# Update Count
$count += 1
}
}
# Print the number of updated users
Write-Host $count "Users have been updated" -ForegroundColor Green
这是我在运行脚本时遇到的错误
Get-ADUser : Error parsing query: 'SamAccountName -eq ADUSER' Error Message: 'syntax error' at position: '20'.
At file_location from CSV.ps1:35 char:6
+ if (Get-ADUser -Filter "SamAccountName -eq $Username" -Server $se ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUse
r
0 Users have been updated
【问题讨论】:
标签: powershell csv scripting active-directory