【发布时间】:2019-10-09 03:11:31
【问题描述】:
我正在使用下面的 powershell 脚本从 CSV 文件创建新的 AD 帐户。我最近为 $extensionAttribute1 和 $extensionAttribute2 添加了变量。我还添加了以下 -OtherAttributes = @{'extensionAttribute1' = $extensionAttribute1;'extensionAttribute2'= $extensionAttribute2}
如何纠正以下错误?
New-ADUser:无法验证参数“OtherAttributes”上的参数。参数为 null 或参数集合的元素包含 null 值。在 D:\OneDrive\Element Care\Powershell\SACRequest - 创建帐户通过 CSV.ps1:62 char:30 + ... -OtherAttributes @{'extensionAttribute1' = $extensionAttribute1}
ps脚本如下:
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv "\\server\path\file.csv"
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$Username = $User.username
$Password = $User.password
$Firstname = $User.'First Name:'
$Lastname = $User.'Last Name:'
$OU = 'OU=CONTRACTORS,OU=ACCOUNTS,OU=organization,DC=domain,DC=lan'
$Descritpion = $User.'Account Type'
$company = $User.'Employer:'
$extensionAttribute1 = $User."Submitter Name" # The employee who originally submitted the request.
$extensionAttribute2 = $User."Submitter email" # The email for who originally submitted the request.
# $email = $User.email
# $streetaddress = $User.streetaddress
# $city = $User.city
# $zipcode = $User.zipcode
# $state = $User.state
# $country = $User.country
# $telephone = $User.telephone
# $jobtitle = $User.jobtitle
# $department = $User.department
#Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, give a warning
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#User does not exist then proceed to create the new user account
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@domain.com" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Lastname, $Firstname" `
-Path $OU `
-City $city `
-Company $company `
-State $state `
-StreetAddress $streetaddress `
-OfficePhone $telephone `
-EmailAddress $email `
-Title $jobtitle `
-Department $department `
-Description $Descritpion `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True `
-OtherAttributes @{'extensionAttribute1' = $extensionAttribute1;'extensionAttribute2'= $extensionAttribute2}
}
}
【问题讨论】:
-
在错误消息中,波浪线似乎在
=符号下划线,这将是无效的。但是您发布的代码不存在=字符。也许你已经修好了?您是否仍然遇到同样的错误? -
显然这与 CSV 值中的空格有关。 extensionAttribute1 是名字和姓氏。我将只使用 extensionAttribute1 和 2 作为第一个和最后一个,并使用 extensionAttribute3 作为电子邮件地址。
-
我会更新原始帖子以包含您当前的错误。
-
我还看到您的字段以冒号结尾,例如
First Name:和Employer:。这些真的是 CSV 中的标题吗?另外,还有错别字:extensionAttrbute1和extensionAttrbute2都缺少i -
谢谢@NathanW 原版现已更新。
标签: powershell csv var