【问题标题】:Import Users into AD Using CSV & PowerShell - ExtensionAttribute2 Issue使用 CSV 和 PowerShell 将用户导入 AD - ExtensionAttribute2 问题
【发布时间】:2015-08-28 17:01:42
【问题描述】:

我正在尝试使用 PowerShell 脚本和 CSV 将用户导入 AD。

这是我的脚本(已删除名称):

$pass = ConvertTo-SecureString "passwordhere" -AsPlainText -Force

Import-Csv .\adimport.csv | New-ADUser -AccountPassword $pass -Enabled $true -ChangePasswordAtLogon $true

Get-ADUser -Filter * -SearchBase 'OU=<ouname>,OU=<ouname>,OU=<ouname>,OU=<ouname>,DC=<name>,DC=<name>,DC=<name>,DC=<name>' -Properties userPrincipalName | foreach { Set-ADUser $_ -UserPrincipalName "$($_.samaccountname)@<domainname>"}

这是我的 CSV 的外观:

Name    Username    SamAccountName  GivenName   Surname DisplayName Description Path    Enabled Postalcode  StreetAddress   City    Title   Office  Company extensionAttribute2 officephone

一切正常,用户被导入(到正确的 OU),所有信息都更新了,除了 extensionAttribute2。

有谁知道在运行脚本时我需要更改哪些内容才能在 AD 中获取 extensionAttribute2 以从 CSV 导入 extensionAttribute2 下的信息?

如果有人需要更多信息,请告诉我。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: powershell csv active-directory


    【解决方案1】:

    要设置扩展属性,请使用以下语法:

     Set-ADUser –Identity $ThisUser -add @{"extensionattribute1"="MyString"}
    

    您还可以使用 New-ADUser 的 -OtherAttributes 参数指定其他属性

    因此,将其集成到您的代码中,我正在尝试创建新用户并同时指定扩展属性。

    Import-Csv .\adimport.csv | New-ADUser -AccountPassword $pass -Enabled $true `
      -ChangePasswordAtLogon $true `
      -OtherAttributes  @{"extensionattribute2"=$_.extensionAttribute2}
    

    让我知道这是否有帮助:)

    【讨论】:

    • 嗨 Fox - 感谢您的回复。只是检查;在 -ChangePasswordAtLogon $true 之后直接添加这个?
    • ie New-ADUser -AccountPassword $pass -Enabled $true -ChangePasswordAtLogon $true -OtherAttributes @{"extensionattribute2"=$_.extensionAttribute2}
    • 你说得对!我所做的只是将您的命令包装起来,使其适合 StackExchange 中允许的 80 个字符。
    • @FoxDeply 好的,所以我试过了,这是我收到的错误消息:New-ADUser:无法验证参数“OtherAttributes”上的参数。参数为 null 或参数集合的元素包含 null 值。 + ... herAttributes @{"extensionattribute2"=$_.extensionAttribute2} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [New-ADUser], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADUser
    • 如果有帮助,我将在工作日使用 extensionAttribute2。我在 CSV 上的 extensionAttribute2 下写的一个例子是;周一至周五上午 9 点至下午 5 点
    猜你喜欢
    • 2015-08-10
    • 2015-06-15
    • 2017-01-17
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2014-03-22
    • 1970-01-01
    相关资源
    最近更新 更多