gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:10:27 25 4
gpt4 key购买 nike

我对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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com