【问题标题】:How do I correctly specify name and attributes when creating users in AD在 AD 中创建用户时如何正确指定名称和属性
【发布时间】:2018-05-04 21:31:39
【问题描述】:

我是 PowerShell 的新手,希望有人能帮助我了解我想要创建的脚本哪里出了问题。

脚本的目的是将名字和姓氏作为强制值,然后存储在 $FirstNames 和 $LastNames 中。然后将它们加在一起并创建 $Samaccountname。 然后将其馈送到 ForEach-Object 循环,为每个提供给 $Samaccountname 的对象创建一个用户帐户,并使用一个为 -otherattributes 提供额外属性的数组。

请看下面我的代码:

  Param
    (
    [Parameter(Mandatory=$True)]
    [string[]]$FirstNames
    ,
    [Parameter(Mandatory=$True)]
    [string[]]$LastNames
    )

    #This will create new users with pre-set attributes based on an array.

    Import-Module ActiveDirectory

    $Samaccountnames = $FirstNames+$LastNames

    $OtherAttributes = @{
    City="Sunderland"
    Department="IT"
    Title='1st Line Analyst'
    #This is the 'Office' attribute
    office='Sunderland IT Building'
    Path='OU=Sunderland,OU=North East,OU=Lab Users,DC=*******,DC=***'
    }

    foreach($Samaccountname in $Samaccountnames)
    {
    New-ADUser -name $Samaccountname @OtherAttributes
    }

这创建了 Samaccount 名称来自 $firstnames 的用户。它也没有应用姓氏属性。

非常感谢

【问题讨论】:

  • 您可能希望提供输入脚本的数据示例;特别是名称的格式

标签: powershell


【解决方案1】:

问题是您正在使用两个数组并尝试将它们添加到彼此。 更多 只取你想要的东西会更有意义:SamAccountName

param(
    [Parameter(Position = 0, Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string[]]
    $SamAccountName
)
Import-Module -Name ActiveDirectory

$attributes = @{
    'City'       = 'Sunderland'
    'Department' = 'IT'
    'Title'      = '1st Line Analyst'
    'Office'     = 'Sunderland IT Building'
    'Path'       = 'OU=Sunderland,OU=North East,OU=Lab Users,DC=*,DC=*'
}

foreach ($Name in $SamAccountName) {
    New-ADUser -Name $Name @attributes
}

【讨论】:

    【解决方案2】:

    由于姓名和姓氏都是必填项 - 它们的数量将相同。 我建议您为 $FirstNames 中的每个名称创建一个新的 samaccountname,并将其添加到 samaccountnames 数组中。

    Param
    (
    [Parameter(Mandatory=$True)]
    [string[]]$FirstNames
    ,
    [Parameter(Mandatory=$True)]
    [string[]]$LastNames
    )
    
    #This will create new users with pre-set attributes based on an array.
    
    Import-Module ActiveDirectory
    
    $Samaccountnames = @() #define $samaccountnames as an array
    
    #for each name in $firstnames we create new samaccountname and add it to samaccountnames array
    foreach ($item in (0..$Firstnames.count)) #
        {
    $samaccountnames += $firstnames[$item]+$lastnames[$item] 
        }
    
    $attributes = @{
    'City'       = 'Sunderland'
    'Department' = 'IT'
    'Title'      = '1st Line Analyst'
    'Office'     = 'Sunderland IT Building'
    'Path'       = 'OU=Sunderland,OU=North East,OU=Lab Users,DC=*,DC=*'}
    
    foreach($Samaccountname in $Samaccountnames)
    {
    New-ADUser -name $Samaccountname @OtherAttributes
    }
    

    【讨论】:

    • 谢谢。您能否逐步阐明以下内容? foreach ($item in (0..$Firstnames.count)) # { $samaccountnames += $firstnames[$item]+$lastnames[$item] }
    • $Firstnames 是一个名称数组,这意味着它可能包含多个值,例如 (Steve,John)。关于 $Lastnames 的相同故事(例如 Smith,Johnson)$firstnames.count 计算名称的数量(在我们的例子中为 2 - Steve 和 John)foreach ($item in (0..$Firstnames.count)) 在这种情况下,$item 表示数组中名称的索引(换句话说,位置) $名字。数组的计数总是从 0 开始。循环中的代码说“获取 $firstnames 中的第一项 (Steve) 并将 $lastnames 数组中的第一项 (Smith) 添加到它。将其保存为数组中的一项 $ samaccountnames"
    • 对不起,我在评论中达到了字符数限制。并且循环不断为$Firstnames 中的每个名称创建新的$samaccountnames 项目,因此$Firstnames 中的下一个项目将是$Lastnames 中的John 和Johnson
    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2011-02-08
    • 2020-01-23
    相关资源
    最近更新 更多