我是PowerShell的新手,希望有人能帮助我了解我想要创建的脚本出错的地方。
脚本的目的是将First和Last名称作为强制值,然后存储在$ 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
}
这创建了来自$ firstnames的Samaccount名称的用户。它也没有应用姓氏属性。
非常感谢
答案 0 :(得分:0)
问题是您正在使用两个阵列并尝试将它们相互添加。如果只需要你想要的东西,那么更多意义: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
}
答案 1 :(得分:0)
由于姓名和姓氏都是强制性的 - 它们的数量是相同的。 我建议你为$ 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
}