powershell - 在AD中创建用户时如何正确指定名称和属性

标签 powershell

我对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。它还没有应用姓氏属性。

非常感谢

最佳答案

问题是您要使用两个数组,并试图将它们彼此添加。只需要您想要的东西就可以了: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
}

关于powershell - 在AD中创建用户时如何正确指定名称和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50164662/

相关文章:

string - 如何延迟 PowerShell 字符串中变量的扩展?

powershell - 如何在powershell中读取machine.config并检查是否存在 key

powershell - 错误变量输出

powershell - Powershell中的webget

windows - 仅在命令行中列出具有路径和文件大小的文件

powershell - 如何在Powershell 4.0 API中将ExecutionPolicy配置为 “RemoteSigned”

powershell - Jfrog CLI 不遵守配置的 API key

powershell - 我怎样才能得到一个任务来设置一个可以在配置中使用的变量?

regex - Powershell根据长度替换行上的文本

powershell - Powershell 2.0的get-CimInstance