【问题标题】:New-ADUser OtherAttributes var from CSV来自 CSV 的 New-ADUser OtherAttributes var
【发布时间】: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 中的标题吗?另外,还有错别字:extensionAttrbute1extensionAttrbute2 都缺少i
  • 谢谢@NathanW 原版现已更新。

标签: powershell csv var


【解决方案1】:

您收到的错误来自您在原始代码中的错字。除此之外,我建议您将Splatting 用于像New-ADUser 这样可以有很多参数的cmdlet。这样一来,您就可以保持代码的可读性和可维护性,并且您不需要使用容易被忽视的反引号字符来续行。

如果您的 CSV 包含所有值,并且所有列标题都如您的代码中所示,那么这样的事情应该可以工作:

# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory

#Store the data from ADUsers.csv in the $ADUsers variable
Import-csv "\\server\path\file.csv" | ForEach-Object {
    #Check to see if the user already exists in AD
    if ((Get-ADUser -Filter "SamAccountName -eq '$($_.username)'" -ErrorAction SilentlyContinue)) {
         #If user does exist, give a warning
         Write-Warning "A user account with username $($_.username) already exist in Active Directory."
         continue
    }
    # only store these in variables as they are used in multiple properties
    $firstName = $_.'First Name:'
    $lastName  = $_.'Last Name:'
    # create a Hashtable with all properties you want to set for the new user
    $properties = @{
        'SamAccountName'        = $_.username
        'UserPrincipalName'     = '{0}@domain.com' -f $_.username
        'Name'                  = '{0} {1}' -f $firstName, $lastName
        'GivenName'             = $firstName
        'Surname'               = $lastName
        'Enabled'               = $true
        'DisplayName'           = '{0}, {1}' -f $lastName, $firstName
        'Path'                  = 'OU=CONTRACTORS,OU=ACCOUNTS,OU=organization,DC=domain,DC=lan'
        'City'                  = $_.city
        'Company'               = $_.'Employer:'
        'State'                 = $_.state
        'StreetAddress'         = $_.streetaddress
        'OfficePhone'           = $_.telephone
        'EmailAddress'          = $_.email
        'Title'                 = $_.jobtitle
        'Department'            = $_.department
        'Description'           = $_.'Account Type'
        'AccountPassword'       = (ConvertTo-SecureString $_.password -AsPlainText -Force) 
        'ChangePasswordAtLogon' = $true
        'OtherAttributes'       = @{'extensionAttribute1' = $_.'Submitter Name';'extensionAttribute2'= $_.'Submitter email'}
        # you can comment out any properties you do not need or are missing in the CSV
        # 'PostalCode'          = $_.zipcode
        # 'Country'             = $_.country
    }
    # create the new user using the properties Hashtable (splat)
    Write-Host "Creating user $($_.username)"
    New-ADUser @properties
}

【讨论】:

  • 喷溅看起来不错,但我不太了解 if 条件的工作原理。是否继续像其他人一样工作?您的最后一个正确创建帐户,但我收到以下错误。 --- Get-ADUser:在“DC=domain,DC=lan”下找不到标识为“xxray”的对象。 At line:7 char:9 + if (Get-ADUser -Identity $_.username) { # SACRequestCSV has ... + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (xxray:ADUser) [Get-ADUser], ADIdentityNotFoundException
  • @nominus 对不起,如果错了。现在应该修好了。 continue 使代码跳转到循环顶部并执行下一次迭代。它为该用户跳过下面的代码。说明here
  • 嗨@theo 感谢您的更新。使用您的和 Scripo 博士的示例,我正在尝试对序列进行故障排除。警告会正确触发,但仍会触发 New-ADUser。我看到 continue 应该将我们带回到 if 语句的顶部,而不是继续执行,而是执行其余代码。如何纠正这种情况?
  • 更多修修补补。如果帐户已经存在,则不处理之后的帐户。如果帐户不存在,则在“DC=domain,DC=lan”下找不到具有身份:“xyz”的对象的 ps 错误。在 line:7 char:9 但随后继续创建帐户。
  • @nominus 啊,显然,如果在Get-ADUser 上使用-Identity 参数,无论-ErrorAction SilentlyContinue 如何都找不到,都会引发异常。我已将测试更改为使用 -Filter 参数来避免这种情况。
猜你喜欢
  • 2019-08-13
  • 2018-05-13
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多